Skip to content

arjunu/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Git Cheatsheet

? - indicates optional arguments
Dangerous commands are marked with ⚠️

Configure

  • Set username
    $ git config --global user.name "<name>"

  • Set email
    $ git config --global user.email "<email address>"

ℹ️ Drop --global option to set local (repo specific) config.

Create/Clone

  • Create new local repo
    $ git init <?project-name>

  • Clone existing
    $ git clone <url>

  • Create from existing code
    $ git init
    Add files & commit
    $ git remote add origin <remote>
    $ git push origin master

Local Changes

  • Add all changes for next commit
    $ git add .

  • List all new & modified files to be committed
    $ git status

  • Show unstaged changes
    $ git diff

  • Commit
    $ git commit -m "<message>"

Update

  • Fetch
    $ git fetch

  • Pull (fetch & merge)
    $ git pull

  • Push
    $ git push <remote-name> <branch-name>

Branches

  • Get current branch name
    $ git branch | grep \* | cut -d ' ' -f2

  • List all existing branches
    $ git branch -av

  • Create new branch
    $ git branch <new-branch-name>

  • Switch to branch
    $ git checkout <branch-name>

  • Carry changes to a new branch
    $ git checkout -b <branch-name>

  • Merge specified branch into current branch
    $ git merge <branch-name>

  • Delete branch
    $ git branch -d <branch-name>

  • Delete unmerged branch
    $ git branch -D <branch-name> ⚠️

  • Rename branch
    $ git branch -m <new-branch-name> # Rename current branch
    $ git branch -m <old-branch-name> <new-branch-name> # Rename another branch

  • Rename remote branch (after above step) [2]
    $ git push origin :old_branch # Delete the old branch
    $ git push --set-upstream origin new_branch # Push the new branch, set local branch

  • Reset branch w.r.t. master $ git reset $(git merge-base master <your-branch>)

Remotes

  • Show all remotes
    $ git remote -v

  • Add remote
    $ git remote add <shortname> <url>

  • Show remote information
    $ git remote show <remote-name>

  • Rename remote shortname
    $ git remote rename <old-shortname> <new-shortname>

Removing Files

  • Delete file from working directory and stage the deletion
    $ git rm <file>

  • Remove file from version control but preserve it locally
    $ git rm --cached <file>

History

  • Show all commits in reverse chronological order
    $ git log

Undoing

  • Remove uncommitted changes
    $ git checkout . ⚠️ # use -f for force

  • Change last commit
    $ git commit --amend

  • Undo last local commit [1]
    $ git reset --soft HEAD~ # soft -- keep your changes
    $ git reset --hard HEAD^ # hard -- discard changes

  • Undo last published commit [1]
    $ git revert HEAD

  • Unstage a file
    $ git reset HEAD <file>

  • Unmodify a file
    $ git checkout -- <file> ⚠️

  • Remove untracked files
    $ git clean -f ⚠️

  • Remove untracked directories
    $ git clean -d ⚠️

  • Reset branch to remote
    $ git reset --hard origin/<branch-name>

Tags

  • Create tag
    $ git tag -a <tag> -m "<message>"

  • Push all tags
    $ git push origin --tags

Cleanup

  • Delete all local branches that no longer have a remote ⚠️
    $ git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}' | xargs git branch -d

Glossary

Files

  • Commited: data is safely stored in your local database

  • Modified: you have changed the file but have not committed it to your database yet

  • Staged: you have marked a modified file in its current version to go into your next commit snapshot

  • Tracked: are files that were in the last snapshot; they can be unmodified, modified, or staged

  • Untracked: files are everything else – any files in your working directory that were not in your last snapshot and are not in your staging area

Repository

  • Remote: Remote repositories are versions of your project that are hosted on the Internet or network somewhere

  • Origin: Default name Git gives to the server you cloned from

Branches

  • master: name given to default branch.

  • HEAD: pointer to current branch.

  • fast-forward: is a type of merge. When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together.

  • Remote tracking branches: are references to the state of remote branches. They act as bookmarks to remind you where the branches in your remote repositories were the last time you connected to them.

  • Rebase: another type of integrating changes (the other one being merge) from one branch into another by taking all the changes that were committed on one branch and replaying them on another.

Sources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published