This post contains the collection of examples of major git cli commands that we normally use in our daily office work. I have grouped them and organised in such a way that it will be easy to remember them and utilise them.
Pictorial representation
Startup
- To turn any
directory
intogit repo
git init
- After that add
remote
of anygithub
repo to link your repo to remote -
or
You can justclone
a repo if you have any
git clone <your github repo url>
To view logs
git log
To check status
of tracked / untracked files
git status
Remotes
List down all the remotes information
git remote -vv
Adding a new remote
git remote add origin git@github.com:mumarkhan999/css-tricks.git
Edit an already added remote
- Here
origin
is the name of theremote
that you wanna edit
git remote set-url origin git@github.com:mumarkhan999/elephant-fight-in-flex-layout.git
Remove
already added origin
- You will replace
origin
with your remote name
git remote remove origin
Fetch
data from remote
git fetch origin
- it will bring branch names, meta data e.t.c.
Branching
List down local branches
git branch
List down all branches
git branch -a
Creating a branch
git branch my_new_branch
git checkout my_new_branch
Creating and checking out branch in 1 step
git checkout -b my_new_branch
Creating a branch from another branch
not from the current one
git checkout -b my_new_branch not_current_branch_name
Checkout to the last branch which you were on without name
git checkout -
Print current branch
name
git rev-parse --abbrev-ref HEAD
Pushing a local branch to a remote branch having different name
git checkout -b my_local_master origin/master
...
git push origin my_local_master:master
You can even push different branch to a different branch on remote
git push origin develop:master
This will make the master on remote to the exact replica of develop from local
Force
push
git push origin branch_name -f
Fetch
and Pull
of branch
-
fetch
will just bring your branch from remote
git fetch origin develop
-
pull
willfetch
as well asmerge
the incoming data intocurrent branch
git pull origin develop
pull
with default which is --merge
- If you pull remote changes with the flag --merge, which is also the default, then your local changes are merged with the remote changes. This results in a merge commit that points to the latest local commit and the latest remote commit.
git pull --merge
pull
with --rebase
- If you pull remote changes with the flag --rebase, then your local changes are reapplied on top of the remote changes.
git pull --rebase
Deleting multiple git branches using regular expression
You can use the git command as mentioned below to delete multiple git branches at once using regular expression
git branch -D `git branch | grep -i "your regular expression"`
List down all the branches information
git branch -vv
Rename
current branch
git branch -m new-name
Rename
if you are on other branch
git branch -m old-name new-name
Delete the old-name remote branch and push the new-name local branch
git push origin :old-name new-name
Reset the upstream branch for the new-name local branch
- Switch to the branch and then
git push origin -u new-name
Deleting
a branch
git branch -D branch_name
-
-D
is for forcefully
Deleting
unnecessary branches of a remote
- This will list down the branches which are available to be
deleted
git remote prune origin --dry-run
- This will delete them, it's better that you run both of these commands
git remote prune origin
Bring specific file from another branch
- Let's say you are on
master
- You wanna bring specific file from
develop
git checkout develop -- lms/djangoapps/courseware/admin.py
Adding
Adding all
- All the
newly
created files areuntracked
one -
git
doesn'ttrack
them - To make them trackable you
add
them individually
git add new1.txt new2.txt
-
or
git add -A
-
or
git add directory/*
Adding only tracked files
- Now let's say there are two types of files in your
working directory
- tracked / untracked
- You only want to
commit
tracked one so
git add -u
- will do the job
Committing
Amend last commit message
git commit --amend -m "New commit message."
Amend last commit without editing its message
git commit --amend --no-edit
Tagging
Moving git tag to the latest commit
Let's say that you have a branch having three commits. On first commit you have a tag, but you wanna move it to the latest one.
git tag -af <tag_name_on_first_commit>
git push origin --tags -f
After this you may need to update the tag/release description on github.
In the above scenerio we moved the tag to the latest commit means the third one, but it is also possible to move it from first to second commit. You just need to specify the commit hash
git tag -af <tag_name_on_first_commit> <commit_hash_of_second_commit>
git push origin --tags -f
Fetching
tags from remote
git fetch --tags --all
Rebasing
Rebasing with other branch
e.g. master
git rebase master
Rebasing (with only specific range of commits)
git rebase --onto <base_branch> <start_commit_hash> <end_commit_hash>
It will create a commit for you (in deattached state) from which you can create a new branch or whatever you want
you can now rebase your with this commit
Squashing your commits
# Here 4 is the number of commits that you wanna squash
git rebase -i HEAD~4
A file will be opened most probably in vim
Now, leave the first commit with p
--> pick
option
and change the rest of commits option to s
--> squash
Save the file, A new file will open having commit messages
Do what ever you want with the commit messages and save it.
Diff
Ignoring
files while running git diff command
git diff my_branch_name ':!*.min.js' ':!*.js' ':!*.po' ':!*.underscore'
Taking diff of two branches
git diff feature1 feature2
Taking diff of current branch with another branch
git diff other_branch_name
Taking diff with stash
git diff stash@{0}
# 0 is the number of stashed changes on the stack
Taking diff with last commit
git diff HEAD~1
Taking diff of a specific file/folder of two different branches
- You need to use
relatvie
path of the file/folder you wanna compare
git diff first_branch second_branch -- myfile.cs
Reset
Undo the changes of single file from staging and un-staging
git checkout <your_file_name>
Wanna move your file from staged to working directory
git reset file_name
- or to move all
git reset
Wanna keep your last committed files (~1) in the working directory but not as committed or staged
git reset HEAD~1
Totally undo all the changes which are in staged or un-staged
git reset --hard
Totally sync
current branch with other branch
- Let's say you are on
local_develop
- You have done some changes and multiple / single comit
- To make
local_develop
just likeorigin/develop
git checkout local_develop
git reset --hard origin/develop
# origin is the name of your remote
Stashing
Don't want to commit but wanna save the changes
git stash
To take back your saved changes
git stash pop
To list down your saved changes
git stash list
cherry-picking
Note: Cherry-pick the commits. Don't cherry-pick the merge.
- In its most basic form, you only need to provide the SHA identifier of the commit you want to integrate into your current HEAD branch
- This way, the specified revision will directly be committed to your currently checked-out branch
git cherry-pick af02e0b
- BUT If you would like to make some further modifications, you can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them
git cherry-pick af02e0b --no-commit
Top comments (1)
Thanks