Cheatsheet of git commands

Get current branch

git branch

Above command will list out all the branches in your current working directory that you checked out. The current active branch will have a asterik mark before it.

Switch to different branch

Say you are working on master branch. Now you need to switch to branch named develop

git checkout develop

Create new branch

Below command will create a new branch locally

git checkout -b newbranch

To push this to remote origin

git push origin newbranch

Delete a branch

Below command will delete branch from local repository

git branch -d branchname

Show latest commit history

git log

Set email and name globally

git config --global ""
git config --global "John"

Set meld as difftool

git config --global diff.tool meld
git config --global difftool.prompt false

View difference between local repository and remote origin in meld

git difftool origin/HEAD master

In above command if you use diff instead of difftool git will show the difference in command prompt

Fetch new branch from remote origin

Say a new branch has been added in the remote repository. You need to fetch the remote new branch in your local git repository

git fetch
git checkout new_branch

Merge single commit from another branch

git cherry-pick ba8151da2af0930ac8dd0bxxxxx492bd51360f9

Revert conflict changes for cherry pick

While cherry picking if there are conflicts and say you want to abort the conflict and cherry picking instead of solving it

git cherry-pick --abort

Revert add

Say you did a git add some_file.txt and now you want to undo it and don’t want to commit.

git reset some_file.txt

Selectively merge

Say you created a branch develop from master

Then there was a commit made to master. Simultaneous commits were made to develop. Now we need to selectively pull changes in develop to master

git checkout feature
git checkout -b temp
git rebase -i master

# Above will drop you in an editor and pick the changes you want ala:
pick 7266df7 First change
pick 1b3f7df Another change
pick 5bbf56f Last change

# Rebase b44c147..5bbf56f onto b44c147
# Commands:
# pick = use commit
# edit = use commit, but stop for amending
# squash = use commit, but meld into previous commit
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

git checkout master
git pull . temp
git branch -d temp

In git rebase -i master the i flag tells git to interactively rebase. Thus it opens a editor. Use the instructions at the bottom of the terminal to edit the file.

Hard revert a commit

git reset --hard abcxyz7

Leave a Reply

Your email address will not be published. Required fields are marked *