Skip to content

Instantly share code, notes, and snippets.

@bikram990
Created January 25, 2022 03:57
Show Gist options
  • Save bikram990/74606e74ca7469006255c2168c737f12 to your computer and use it in GitHub Desktop.
Save bikram990/74606e74ca7469006255c2168c737f12 to your computer and use it in GitHub Desktop.
Git Commands Cheatsheet

Cloning and getting remote changes

Clone a repository

git clone <gitRepositoryURL>

Clone a repository with limited history

git clone --depth n <gitRepositoryURL>
where n is the number of revisions you want in the history

Clone specific branch

git clone -b <branchName> --single-branch <gitRepositoryURL>

Pull remote changes

git pull

Pull remote changes without generating a merge commit

git pull --rebase

Discarding changes

Discard changes to a file

git checkout <filePath>
Note: you will loose all the changes to that file

Discard some commit

git reset --hard HEAD~n
where n is number of last commits which you want to discard
Note: you will loose all the changes

Tags

List all tags

git tag -l

Create a local tag

git tag <tagName>

Create a remote tag

git tag <tagName>
git push origin <tagName>

Push all local tags to remote

git push origin --tags

Delete a tag

git tag --delete <tagName>

Branches

List all branches

git branch -a

List all remote branches

git branch -r

List all local branches

git branch -l

Switch to a branch

git checkout <branchName>
Note: if there is a tag with same name as the branch then this command will checkout the tag and will not track it

Checkout remote branch with a different local name

git branch -f --track <localBranchName> origin/<remoteBranchName>

Create a local branch

git checkout -b <newBranchName>

Create a local branch and push it to remote

git checkout -b <newBranchName>
git branch --edit-description
git push origin <newBranchName>
Note: --edit-description is for editing the commit message for the branch creation

Create a local branch from a specific commit

git checkout <commitHash>
git branch -b <newBranchName>
git push origin <newBranchName>

Rename a local branch

git branch -m <oldName> <newName>
git branch -m <newName> #rename current local branch

Rename remote branch

git branch -m <oldName> <newName> #rename locally
git push remote :<remoteBranchName> #delete the branch from remote
git push remote <newLocalName> #push the local branch to remote

Delete a local branch

git branch -d <branchName>
or
git branch -D <branchName>
Note: -D is forceful deletion of branch

Delete a remote branch

git push origin --delete <branchName>
or
git push origin :<branchName>

Commit and Push

Commit changes

git add <changedFiles>
git commit -m <commitMessage>

Push local commits to remote

git push

Change commit message

git rebase -i HEAD~n
where n is number of last commits which you want to rebase
Note: The above command will open a editor and will give various rebase options use reword option to select the commits whose commit message you want to change. After selecting the commits close the file. git will open all the commits one by one to allow rewording the commit message.

View changes in commit

git show <commit hash>
Note: You can get the commit hash from git log command

Undo last commit

git reset --soft HEAD~n
where n is number of last commits which you want to undo

Cherry Pick a commit from other branch

git checkout <branch to apply the commit>
git cherry-pick <commit hash>
Note: You can get the commit hash from git log command

Merge

Merge without committing

git merge --no-commit --no-ff <branchNameFromWhereYouWantToTakeChanges>

Handling merge conflicts

git checkout --ours <filePath>
git checkout --theirs <filePath>
Note: if you want to merge both the changes then change the file and add the file to the commit using git add

Abort a merge

git merge --abort

List all the commits in a merge

git log <MergeCommitHash>^..<MergeCommitHash> --pretty=format:"%aD %H %ae %s"

Revert

Revert a specific commit

git revert -m 1 <commitHash>
Note: 1 indicates that you want to go to first ancestor of this commit

Remove a big file from a specific commit pushed to remote

git rebase -i <commitHash - 1> # the commit hash of one commit before the commit which has the big file it will open editor, change *pick* to *edit* for the wrong commit which has the big file and close the editor
git rm --cached <Path to bigFile>
git commit --amend -C HEAD
git rebase --continue
git push --force

Stash

Stash local uncommitted changes

git stash
Note: all the files added to a commit will be un-staged and put in stash

List all stashes

git stash list

Apply stashed changes

git stash pop
Note: this will apply the stash and delete the stash changes

Apply stashed changes to multiple branches

git stash apply
git checkout <branchName>
git stash apply
git stash drop
Note: last step will delete the stash

View diff from stash without applying

git stash show -p stash@{n}
where n is the stash number you want to view, to get the stash number use git stash list command

Commit Logs

Show all commit logs

git log

Show all of your commits

git log --author='Your Name'
or
git log --author='you@somedomain.com'

Show all unpushed commits

git log --branches --not --remotes
Note: the above command will list all the unpushed commits in all branches

Show all commits which are present in one branch and not in other

git log <branch> --not <branchToCheckAgainst> --reverse --author="user@yourdomain.com" --pretty=format:"%aD %H %ae %s"

GIT miscellaneous

See info

git remote show origin

Change origin URL

git remote set-url origin <URL>

Update user details

git config --global user.name 'Your Name'
git config --global user.email.you@somedomain.com

Add a file to a commit

git add <filePath>

Un-stage a file (remove a file from the commit)

git reset HEAD <filePath>

Un-stage all files (remove all files from the commit)

git reset HEAD --

Delete a file

git rm <filePath>
git rm -f <filePath>
Note: -f is for forcefully deleting the file

Change email id in already commited commits

git filter-branch --env-filter '
WRONG_EMAIL="<wronguser@wrongdomain.com>"
NEW_NAME="Your Name"
NEW_EMAIL="correctuser@correctdomain.com"

if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment