Git Cheat Sheet, Tips and Best Practices

Even though there are many different Git visual tools available for us to use it is not always that we have a visual tool available and have to open a terminal window to use Git.

Unfortunately, the longer we use Git visual tool, the more there are chances we forget how to use some of Git commands and their command line options and switches.

On this page I will list all of the most commonly used Git commands that I use myself and will refer to this page whenever I am not very sure how to use one of them :).

Starting up with Git

Clone an existing repository

git clone ssh://user@domain.com/repo.git

Create new local repository

git init

Working with Branches

List all branches (both local and remote)

git branch -a

List all branches (both local and remote)
Also, shows the last commit message and if it is forward or behind

git branch -av

List local branches and which remotes they track

git branch -vv

Create a new branch based on current HEAD

git branch <branch-name>

Create a new tracking branch based on a remote branch
A ‘tracking branch‘ in Git is a local branch that is connected to a remote branch. When you push and pull on that branch, it automatically pushes and pulls to the remote branch that it is connected with.

git checkout --track <remote/branch>

Switch to a different branch

git checkout <branch-name>

Rename current branch 

git branch -m <new-branch-name>

Delete local branch

git branch -d <branch-name>

Delete remote branch

git branch -dr <remote/branch>

or you can also do:

git push origin :<branch-name>

or you can also delete remote branch this way:

git push origin --delete <branch-name>

Reset and go back to the latest version of master

git reset --hard origin/master

Update your local branch with the latest from Master

git checkout master 
git pull   
git checkout <you-branch-name>
git rebase master

Merge your local branch into Master and Push

git checkout master  
git merge --ff-only <your-local-branch> 
git push

Force push your branch

git push -f origin <branch-name>

Local Changes

List updated files in your working directory

git status

See what has changed in selected file

git diff <file-path>/<file-name>

Stage new and modified files

git add .

Stage a specific file only 

git add <file-name>

Stages modified and deleted

git add -u

Stages all

git add -A

Commit changes with a message

git commit -m "Short description of this commit"

Update message of a previous commit

git commit --amend

For example:

git commit --amend -m "Added new line #4"

Publish your local branch to remote

git push -u origin <branch-name>

Remove file from being tracked: 

git rm --cached sites/default/settings.php

List all stashes

git stash list

Reapply a specific stash from the list of stashes

git stash apply stash@{2}

Temporarily shelve (or stash) changes

git stash

Temporarily stash changes including new/untracked files

git stash -u

Reapply previously stashed changes

git stash pop

 Reapply previously stashed changes and keep them in your stash

git stash apply

Stash changes with a spcific message

git stash save "Your message here"

Delete file from local Git repository, but keep it in your working directory as an ignored file

git rm --cached <file-name>

Delete file from local Git repo AND from local file system

git rm <file-name>

Committing an ignored file

git add -f <file-name>
git commit -m "Force adding file which is on the list of ignored files"

Take specific, single commit from one branch and apply it onto another

git cherry-pick <commit-hash>

Remove untracked files at specified path only

git clean -f <path>

Remove untracked files and directories

git clean -df

 

Merging or Squashing Commits

If you have 2 or 3 latest commits and would like to merge or squash them into a single commit with a new message, you can you the command below. The postfix the at end of HEAD~ is the number if commits you would like to merge together into a single one.

git reset --soft HEAD~2 && git commit

 

Undoing Changes

Return to old version of the file

git checkout <file-name>

Check out a file from a specified commit

git checkout <commit> <file-name>

and If you decide you don’t want to keep the this version of the file, you can then check out the most recent version you have with the following command:

git checkout HEAD <file-name>

Update all files in the working directory to match the specified commit

git checkout <commit>

Undo the changes introduced by the commit
Generate a new commit that undoes all of the changes introduced in <commit>, and then apply it to your current branch.

git revert <commit>

Unstage the specified file but keep the changes made

git reset <file>

Reset the staging area and keep the changes made

git reset

Reset the staging area and override the changes

git reset --hard

Reset the staging area to a specified commit

git reset <commit>

Permanently undo committed snapshots up to specified commit

git reset --hard <commit>

Remove untracked files from your working directory

git clean -f

Show you which untracked files are going to be removed without actually removing it 

git clean -n

 

Working with Tags

In git you can tag specific points in history as being important. Developers use this functionality to mark release points. For example v1.0 or v1.2.3 and so on. And then you can create a new branch at a specific tag with git checkout -b <branch-name> <tag-name>

Mark current commit with a tag

git tag <tag-name>

Tag a specific commit 

git tag <tag-name> a4db8cc
git push --tags

List all tags

git tag

Checkout by specific Tag

git checkout tags/<tag name>

 

Git Best Practices

  • Agree with your team on a Workflow
  • Commit Often
  • Don’t Commit Half-Done Work
  • Test Before You Commit and spend an extra minute to look at the changes you made with git diff before you make a commit
  • Do not use git add . all the time. Consider using git add [file-name] to make sure you stage only the files you indeed mean to commit
  • Write short but still descriptive commit messages
  • Only use git reset to undo local changes—you should never reset snapshots that have been shared with other developers
  • Keep master branch as to be always production-like and deployable
  • To work on a new feature, create a new branch out of a dev branch. When feature is ready, merge feature branch back to dev branch. When dev is well tested and ready, merge dev branch into production-like master branch.
  • For immediate and urgent fixes found in production, branch off a master branch to work on a fix. When fix is ready merge it back to master branch.
  • To keep your feature branch fresh and up to date with the latest changes in master, use rebase

If you are interested in learning more about how to work with Git, you can check out these video courses below:

Version Control

There is just no way you can successfully manage your app and its releases without a good knowledge of version control system like Git. Every developer you join or every software team you join to work on an app will require you to be able to work with a version control system. It is a must to have knowledge and I suggest you learn it and the sooner the better.

Git Complete: The definitive, step-by-step guide to Git. Video Course.

icon icon

Git Complete Mastery with GitHub : 100% Hands-on Git Video Course

iconGit Complete Mastery with GitHub

Git Going Fast: One Hour Git Crash Video Course

iconGit Going Fast: One Hour Git Crash Course

Leave a Reply

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