Git survival cheat sheet


First, introduce yourself

git config --global "Nikolay Neupokoev"
git config --global ""
git config --global core.editor vim # or nano
git config --global init.defaultBranch master

Create your local repo and publish

Find a gitignore file for your stack on

git init
git add .
git commit -m "Initial commit"

After you created a remote repo on GitHub, GitLab, Bitbucket etc

git remote add origin <url to git repo>
git push -u origin master

Get someone's repo

git clone <url>

Some projects like boost, Qt, and user dotfiles use submodules

cd <project name>
git submodule update --init

Start coding

Create a separate branch

git checkout -b <branch name>

Branch name conventions

  • git flow (feature/new-button fix/wrong-color release/1.0.2)

  • GitHub flow (some-meaningful-name)

Commit your work

git add -p
git commit

Have more changes to previous commit? add and

git commit --amend

Want to undo the last commit?

git reset HEAD^

In any unexplainable situation do not panic and use

git status

it always explains where you are and gives you options of what you can do.

Finish your work

Now you are ready to publish your changes and make a Pull Request

git push origin branch-name-with-your-work

If you use Pull Requests on GitHub or BitBucket then branches will be merged automatically when the Pull Request is accepted. But if you are just messing with branches then merge your branch into master

git checkout master
git merge branch-name-with-your-work

and delete your branch

git branch -d branch-name-with-your-work

And then push only the master branch.

git push origin master

It makes sense to push your branch to the remote if someone else in your team needs these changes before they are approved, or for backup purposes if the task takes longer than a day and you think that your work can blow up your computer.

In any case, do not forget to clean up after yourself on the remote. When the branch was merged to master, delete that thing that was pushed before.

git push --delete origin branch-name-with-your-work

When someone else does this in your team, this is when you need

git fetch -p origin

But more context about this later.

Sync work with colleagues

Get new updates from a remote repo. In simple cases, one pull is enough

git pull

but sometimes you need

git checkout master
git fetch -p origin # download remote changes only (+ update info about removed branches on the remote). if there is nothing new then stop here, no need to pull
git stash # in case there are local staged/unstaged changes, we hide them
git pull
git stash pop # return hidden changes

In case you already did some local commits, and there are remote changes, and you prefer straight-forward git history then rebase your changes on top of remote commits

git pull --rebase

The situation is simpler if you work in your branch. Then you pull easily and then rebase. To move your changes on top of the latest repo state

git rebase master # when current branch is branch-name-with-your-work
git rebase master branch-name-with-your-work # when current branch is something else, master, for example

Bonus trick

Restore a file once deleted

git rev-list -n 1 HEAD -- <file>
git checkout <hash from the previous command>^ -- <file>

Rate this page