Skip to content

Latest commit

 

History

History
227 lines (168 loc) · 5.26 KB

File metadata and controls

227 lines (168 loc) · 5.26 KB

1) Status of States

Current status of unstaged files

git status

History of commits

git log

2) Branching

Whenever you are working on feature or bug, always work on another branch & not on the main branch.

Create a new branch

git branch <new-branch-name>

Change the head to other branch

git checkout <branch-name>

Head is where the current pointer is, all the commits happen at Head pointer, so inorder to commit to other branches, you must switch branches.

Do this before creating a new branch (to copy latest contents of main branch)

Step 1: Switch to your local main

git checkout main

Step 2: Pull the latest changes from the original repo (upstream)

git pull upstream main

Step 3: (Optional) Push changes to your fork

git push origin main

Step 4: Now create the new branch from the updated main

git checkout -b new-feature

Note:

  • Always have separate branch for each feature/bug you're working on.
  • If a PR is opened on one branch, then all the commits on that same branch will be grouped under the same PR, which means 1 branch = 1 pull request only.
  • While creating a new branch always make sure it is up to date with the main branch of original repo

3) Make changes in the state

A. Staging

Stage the changed made in all files

git add .

Stage the changes made in specific files

git add <file_name>

B. Unstage

Unstage the changes made in all files

git restore --staged .

Unstage the changed made in specific files

git restore --staged <file_name>

4) Saving & restoring the state

A. Commiting

Commit the changes

git commit -m "<description_of_commit>"

B. Uncommit

Restore to a specific commit state

git reset <hash_value_of_commit_whose_state_is_to_be_restored>

Say you have 3 commits, and you want to remove the last 2 commit, for that you need to mention the hash value of the 1st commit, and the codebase will restore to 1st commit's state.

C. Stashing

When you want to save the state without committing

git add . 
git stash 

D. Restore from Stash

When you want to bring back the saved state (backstage -> front)

git stash pop

This will bring the saved state back to the current state

E. Clear Stash

Remove the saved state from the stash

git stash clear

F. Squashing Commits

Combine multiple commits into a single commit

 git rebase -i <hash-of-commit-from-which-commits-are-to-be-merged>

now for every commit you want to merge, say a interactive dashboard is shown with these commits:

pick 1
pick 2 
pick 3
pick 4 

now if you want to merge 2 & 3 into 1 then do this:

pick 1
s 2
s 3
pick 4

Basically wherever you mention s, the previous commit is taken as the commit & the commit with "s" get merged into the previous one. exit with "x"

Then you will be prompted to write the commit message. write. exit with "x".

Then, git push origin temp

5) Working with GitHub

A. Self Project

Connecting repo to git

git remote add origin <github-url-with-git-suffix>

where, remote = declares working with URL add = adding new URL origin = name of URL to be added (all repo that are in personal account are by default named as origin )

Display all URL's attached to git

git remote -v

Push commits to GitHub

git push origin main

where, origin = url to which commits are to pushed main = branch to push at

B. Open-source contribution

High level steps

  1. Go to the repo, fork it.
  2. On local machine, clone your own forked repo.
  3. Create a new branch for each feature/bug you're working on.
  4. Change the head to the branch you're working on.
  5. Make changes, commit the changes.
  6. Open a PR from Github for repo owner/maintainers to review.
  7. If any changes recommended, make the changes in the same branch, commit & push the changes.
  8. If your contribution is good enough, without any conflicts, if would be merged.

Cloning a repo

git clone <github-repo-link-with-git-suffix>

Upstream URL for a forked repo

You can add the original URL in git for the forked repo. The original URL is the URL of the organisation's repository from where you have forked the repo.

git remote add upstream <url>

Make your forked repo up-to-date with original repo main branch

  1. Change the branch to main

  2. git pull upstream main (gets all the changes, commits locally)

  3. git push origin main (push to github)

     						OR
    
    1. Change the branch to main
  4. fetch all commits on all branch, prune enables the ones that are already deleted git fetch --all --prune

  5. Reset main branch of origin with the main branch of upstream: git reset --hard upstream/main

  6. git push origin main (push to github)

  7. git log, you can see the updated commits

C. Merge conflict

You "raghav" made a change in line 3, someone else "rohan" also changed line 3.

now rohan's PR is merged.

maintainer will see a merge conflict, since both of you tried to change the same line of code. it needs to be manually specified that whose changes are to be over-written & then the pull request is merged.