Git Cheat Sheet

Git Cheat Sheet

Git Cheat Sheet

01. Git Configuration

To set the name that will be attached to your commits

>>  git config --global user.name “Your Name”

To set the email that will be attached to your commits

>> git config --global user.email “email@you.com”

02. Start a Project

To create a new local repository in the directory

>> git init

If you want to clone or download a repository

>>  git clone [paste the .git url here]

03. For common Day to Day Work

To display the status of your directory (which includes details of newly added or removed files, changes made in file and branches, etc)

>> git status

To add a file to the staging area

>>  git add [file]

To see the changes between the working directory and staging area

>> git diff [file]

To discard the changes in the working directory

Once you discard the changes you cannot recover

>> git checkout -- [file]

To revert back your repository to the previous state

>> git reset [file]

To create a new commit from the changes added

>>  git commit

To remove a file from the working directory

>>  git rm [file]

04. Git Branch

To list all the branches in the repository

>> git branch [-a]

To create a new branch in the repository

>> git branch [branch_name]

To switch from one working directory to a branch

>> git checkout [-b] [branch_name]

To join branch

>> git merge [from name]

To remove a branch from the repository

>> git branch -d [name]

05. Checking Log

To list the commit history

>> git log [-n count]

To list the commit history with a history graph

>>git log --oneline --graph --decorate

06. Tagging

To list all tags

>> git tag

To create a new tag reference name for the commit

>>git tag [name] [commit sha]

To remove a tag from a repository

>> git tag -d [name]

07. Reverting changes

To switch the current branch to the target reference

>>  git reset [--hard] [target reference]

To create a new commit, reverting all changes from the specified commit

>> git revert [commit sha]

08. Synchronizing repositories

To fetch changes from the remote

>> git fetch [remote]

To delete remote ref's

>> git fetch --prune [remote]

To fetch changes from the remote

>> git pull [remote]

To push the local changes to remote

>> git push [--tags] [remote]

To push a local branch to remote

>> git push -u [remote] [branch]

09. Ignoring Files in Git

>> cat .gitignore

write all the file names you want to not commit or push