DEV Community

Cover image for Git bloom
Muhammad Umar Khan
Muhammad Umar Khan

Posted on • Updated on

Git bloom

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

image
image
image

Startup

  • To turn any directory into git repo
git init
Enter fullscreen mode Exit fullscreen mode
  • After that add remote of any github repo to link your repo to remote
  • or You can just clone a repo if you have any
git clone <your github repo url>
Enter fullscreen mode Exit fullscreen mode

To view logs

git log
Enter fullscreen mode Exit fullscreen mode

To check status of tracked / untracked files

git status
Enter fullscreen mode Exit fullscreen mode

Remotes

List down all the remotes information

git remote -vv
Enter fullscreen mode Exit fullscreen mode

Adding a new remote

git remote add origin git@github.com:mumarkhan999/css-tricks.git
Enter fullscreen mode Exit fullscreen mode

Edit an already added remote

  • Here origin is the name of the remote that you wanna edit
git remote set-url origin git@github.com:mumarkhan999/elephant-fight-in-flex-layout.git
Enter fullscreen mode Exit fullscreen mode

Remove already added origin

  • You will replace origin with your remote name
git remote remove origin
Enter fullscreen mode Exit fullscreen mode

Fetch data from remote

git fetch origin
Enter fullscreen mode Exit fullscreen mode
  • it will bring branch names, meta data e.t.c.

Branching

List down local branches

git branch
Enter fullscreen mode Exit fullscreen mode

List down all branches

git branch -a
Enter fullscreen mode Exit fullscreen mode

Creating a branch

git branch my_new_branch
git checkout my_new_branch
Enter fullscreen mode Exit fullscreen mode

Creating and checking out branch in 1 step

git checkout -b my_new_branch
Enter fullscreen mode Exit fullscreen mode

Creating a branch from another branch not from the current one

git checkout -b my_new_branch not_current_branch_name
Enter fullscreen mode Exit fullscreen mode

Checkout to the last branch which you were on without name

git checkout -
Enter fullscreen mode Exit fullscreen mode

Print current branch name

git rev-parse --abbrev-ref HEAD
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You can even push different branch to a different branch on remote

git push origin develop:master
Enter fullscreen mode Exit fullscreen mode

This will make the master on remote to the exact replica of develop from local

Force push

git push origin branch_name -f
Enter fullscreen mode Exit fullscreen mode

Fetch and Pull of branch

  • fetch will just bring your branch from remote
git fetch origin develop
Enter fullscreen mode Exit fullscreen mode
  • pull will fetch as well as merge the incoming data into current branch
git pull origin develop
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"`
Enter fullscreen mode Exit fullscreen mode

List down all the branches information

git branch -vv
Enter fullscreen mode Exit fullscreen mode

Rename current branch

git branch -m new-name
Enter fullscreen mode Exit fullscreen mode

Rename if you are on other branch

git branch -m old-name new-name
Enter fullscreen mode Exit fullscreen mode

Delete the old-name remote branch and push the new-name local branch

git push origin :old-name new-name
Enter fullscreen mode Exit fullscreen mode

Reset the upstream branch for the new-name local branch

  • Switch to the branch and then
git push origin -u new-name
Enter fullscreen mode Exit fullscreen mode

Deleting a branch

git branch -D branch_name
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • This will delete them, it's better that you run both of these commands
git remote prune origin
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Adding

Adding all

  • All the newly created files are untracked one
  • git doesn't track them
  • To make them trackable you add them individually
git add new1.txt new2.txt
Enter fullscreen mode Exit fullscreen mode
  • or
git add -A
Enter fullscreen mode Exit fullscreen mode
  • or
git add directory/*
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • will do the job

Committing

Amend last commit message

git commit --amend -m "New commit message."
Enter fullscreen mode Exit fullscreen mode

Amend last commit without editing its message

git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Fetching tags from remote

git fetch --tags --all
Enter fullscreen mode Exit fullscreen mode

Rebasing

Rebasing with other branch e.g. master

git rebase master
Enter fullscreen mode Exit fullscreen mode

Rebasing (with only specific range of commits)

git rebase --onto <base_branch> <start_commit_hash> <end_commit_hash>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Taking diff of two branches

git diff feature1 feature2
Enter fullscreen mode Exit fullscreen mode

Taking diff of current branch with another branch

git diff other_branch_name
Enter fullscreen mode Exit fullscreen mode

Taking diff with stash

git diff stash@{0} 
# 0 is the number of stashed changes on the stack
Enter fullscreen mode Exit fullscreen mode

Taking diff with last commit

git diff HEAD~1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Reset

Undo the changes of single file from staging and un-staging

git checkout <your_file_name>
Enter fullscreen mode Exit fullscreen mode

Wanna move your file from staged to working directory

git reset file_name
Enter fullscreen mode Exit fullscreen mode
  • or to move all
git reset
Enter fullscreen mode Exit fullscreen mode

Wanna keep your last committed files (~1) in the working directory but not as committed or staged

git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Totally undo all the changes which are in staged or un-staged

git reset --hard
Enter fullscreen mode Exit fullscreen mode

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 like origin/develop
git checkout local_develop
git reset --hard origin/develop
# origin is the name of your remote
Enter fullscreen mode Exit fullscreen mode

Stashing

Don't want to commit but wanna save the changes

git stash
Enter fullscreen mode Exit fullscreen mode

To take back your saved changes

git stash pop
Enter fullscreen mode Exit fullscreen mode

To list down your saved changes

git stash list
Enter fullscreen mode Exit fullscreen mode

cherry-picking

Note: Cherry-pick the commits. Don't cherry-pick the merge.
image

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bgrand_ch profile image
Benjamin Grand

Thanks