Most common Git commands for daily work

Other techs
Sponsored links

This article contains most commonly used Git commands. I think this list is enough for daily development.

Sponsored links

git clone and checkout

How to clone new repository

When we join to a team there’s probably a repository on which we work. Then, start with these commands. CI server may start build for the new branch if we create it on a browser. It is better to create new branch on the local machine.

git clone <url to the git repository>
cd top-directory
git checkout -b "new-branch-name"

How to create new branch

When we already have a cloned repository.

git checkout master
# should update the contents first
git pull
git checkout -b "new-branch-name"

If we want to create a branch based on another branch

git checkout <where-you-want-to-use-as-base-branch>
git pull
git checkout -b "new-branch-name"
Sponsored links

git add, commit and reset

How to add a commit

Firstly, we need to add files which we want to commit.

# To add all files
git add -A
# To add a specific file
git add <file name>

We should confirm if only necessary files are added there.

git status

Then, commit and push it to remote repository.

git commit -m "commit message"
git push

How to add files to last commit

Sometimes I forget to add some files. Then, do amend. It puts newly added files to the last commit. git opens an editor to edit the commit message. Change the commit message if necessary.

git add <additional files>
git commit --amend

git rebase

How to rebase onto a branch or abort it

Since master branch is updated by other developers while developping we need to update our own dev-branch too. Run this command when we want to rebase onto master.

git pull --rebase origin master

If we want to rebase onto another branch

git pull --rebase origin "based-branch-name"

How to proceed with conflicts

When rebase causes conflicts, we need to resolve the conflicts then do one of following.

# after resolving the conflicts
git rebase --continue
# if there's no conflicts
git rebase --skip

How to abort the rebase

We need to abort it if we start rebasign onto incorrect branch.

git rebase --abort

When pushing a change to the remote repository by mistake (reflog and reset)

I sometimes rebase onto incorrect branch. Even worse, I push it to remote repository. In this case, we can run following commands. reflog shows full Git history. If we rebased before reflog command, it shows lots of commit numbers because rebase command does commit one by one. Choose correct commit number carefully.

# Find a hash number where you want to go back
git reflog
# Go back to the commit
git reset --hard <hash number where you want to go back>

git diff

How to compare changed files

When we want to show the difference.

git diff <branch-name-to-compare>

Showing diff for specific file

git diff <branch-name-to-compare> -- <file-name>

When we want to see which files are updated

git diff <branch-name-to-compare> --name-only

git stash

How to store modified files

We often need to change working branch but we don’t want to lose modified files. Then, we can store them by stash but new files are not included by default. Choose one of commands which you want to desire.

# without new files
git stash
# including new files
git stash -u
# add comment
git stash save -u "able to add comment for the stash"

How to apply the stashed changes

When we want to apply the latest stash changes and remove it from the stash list.

git stash pop

If we don’t want to remove it.

git stash apply

If we need to apply one of specific stashed changes.

# check the stash list first
git stash list
# take the specific one
git stash apply stash@{2}
# take the specific one and remove it from the stash list
git stash pop stash@{2}

git log

When we want to check the commit history with the corresponding messages, author, date and commit number

git log

I think author and date are not necessary most cases. Following command shows it more compactly.

git log --oneline

git reset

How to cancel commits

When we did a commit by mistake and don’t want to remove the change this command cancel it.

git reset <last commit number>

If we want to cancel the commit and want to remove all changes.

git reset --hard <last commit number>

How to resolve conflict when pulling for code review

When we do git pull for code review it can bring code conflicts. Don’t try to resolve by yourself. Run this command instead.

git reset --hard <latest commit number of the branch which we want to review>

git branch

How to show branch list that includes a keyword

Git doesn’t provide a feature to search branches that include keyword. However, we can use grep if the terminal supports it to show the list.

git branch -a | grep "keyword"

How to remove a branch

git branch -D "branch-name"

git cherry-pick

This command applies commits from another branch to destination branch. This is useful for example when we fix a bug and merged it onto released branch and we need to apply the changes to main/master branch.

How to pick one commit

git cherry-pick <commit number that we want to apply>

How to pick multiple commits

It’s possible to apply multiple commits at once by following command.

git cherry-pick <start commit number>..<end commit number>

Note that the commit specified first here isn’t applied. We need to specify commit number that is one before the commit that we actually want.

For example, if we want to apply 4 commits from 9e308f4 to 19e7ef8 in the following case

$ git log --oneline
19e7ef8 (HEAD -> master) refactor  // <---- want to apply to here
5e479b4 update typescript
c0097a6 Add array-type
9e308f4 update arg-value  // <---- want to apply from here
3e4dfce update readme  // <-- specify this as start commit number

git command looks like this

git cherry-pick 3e4dfce..19e7ef8

Cleaning up local branches

Delete branches that no longer exists in remote

When using git for a long time, number of garbage branches increases in local repository. This is because the local branches are not deleted automatically when they are merged in remote repository. Those branches’ name is for example “remote/feature/xxx”. These branches remain in local repository. git fetch or git pull with prune option removes those branches.

git fetch --prune
git pull --prune

Delete branches that have already merged

The commands above remove “remote/feature/xxx” but not “feature/xxx”. When we want to remove those branches as well, we need another command.

Firstly we need to get branch list that have already merged onto main branch. Let’s get the list following command. Adjust “main” depending on your repository.

$ git branch --merged main
  feature/aaa
  feature/bbb
  feature/ccc
* main

Its result contains “* main” branch. We need to get rid of that from the result by using grep.

$ git branch --merged main | grep -v '* main$'
  feature/aaa
  feature/bbb
  feature/ccc

Last step is passing the result to deleting command.

git branch --merged main | grep -v '* main$' | xargs git branch -d

Adjust the search string specified for grep command.

Comments

Copied title and URL