Git Cheat Sheet

I just collected some git-commands in this cheat sheet you would want to use as a Visual Studio Developer who is new to git and for a start only wants to get some work done. Visual Studio 2017 already has a good basic support for git but when it comes to merge-conflicts or managing different branches and states it can be a bit bumpy.

For example: When merge conflicts occur when pulling with Visual Studio then the pull request is aborted without any details. If you use the git-bash command line and pull from the repo you get a more detailed information about what went wrong. And mostly you will find some merge conflicts that need to be resolved.

I also feel that the official git-documentation is quite lengthy and you need some time to figure out what you really need. Therefor I started creating my own little cheat sheet with my most commonly used git commands – which I want to share with you in this article.

When you are new to git, maybe coming from the Microsoft Source Control-World, I suggest you really get to know the basic ideas behind git and its functionality. As much benefits git brings to you, it can get really messy if you start branching, merging and re-basing with more than one developer. So really be thoughtful about your branching, merging and pulling strategies and keep the discipline in your team high.

Here are some basic rules I try to keep in mind when working with git:

  • keep the amount of code in a commit small, and it should form a logical unit
  • introduce a new function and its corresponding unit-test jointly in one commit
  • commit code changes made in context of a task
  • add meaningful commit messages so others (and maybe you yourself) can understand what you have been doing and why
  • create small feature branches and re-base/merge regularly so you do not have large merging sessions which are frustrating and cost a lot of unnecessary time

Handy cheat sheet for git commands

Clone a remote repository

Clone with ssh

Cloning a repository actually creates a local copy whith which you can work. It depends on weather you ar

git clone https://URLtoRepo

Adding files

Update all files with changes

Before updating your changes be sure to have added your files to git, so git knows about them.

git add -u

This adds the file to the index. After a git-add you still need to locally commit and perhaps push your changes to the server.

Add all files to the index

This command also adds untracked files!

Warning: Could add files that were not intended to!

git add -A

Official git-add* documentation.

Handling Commits

Commit changes with a commit message

If you are trying to execute a commit and forget to insert the -m parameter for entering a commit message you might end in the standard text editor.

To commit all your local changes without opening the editor.

git commit -m "insert commit message here"

To test which files will be committed use –dry-run:

git commit --dry-run

Commit all files git knows about:

git commit -am "insert commit message here"

 Exit git commit editor

If you are doing a commit without the -m in the git shell and you have not configured a different text editor the „linux“ Editor Vim is used to insert the commit message.
It can be tricky to exit the editor. Here is how to close it:

Shift + z,z or <esc> :wq <enter>

Set notepad as the default git editor

git config --global core.editor "notepad"

The handling with the text editor seems a bit weird. So, don’t be confused. From the command line notepad is opened, you insert the commit message there and then close it again. The console gets hand of the inserted text and uses it for the commit.

Show the change log

These commands come in handy when you want to know all committed changes.

git log

Make the log compact and readable:

git log --oneline

Additionally, show the branching graph:

git log --oneline --graph

Official git-log* documentation.

Lists authors and commit messages – summarized the log grouped by author and title:

git shortlog

Official git-shortlog* documentation.

Show the last changes made:

git show HEAD

 

Checkout or delete branches or files

You can either checkout branches or paths/files. There is a difference when using the commands. Some additional parameters work on the one or the other. So be aware of that.

Checkout a branch

git checkout <branchname.here>

Revert the change of a specific file

If you changed a local file and want to revert the changes to the repo „HEAD“ then use:

git checkout <filename.here>

Official git-checkout* documentation.

Reset all local changes

Reset all local changes – meaning – all local changes are removed and the HEAD of the current branch is fetched:

git reset --hard

Take the last commit out of the repository and add it to the staging area meaning you can make changes to the code again:

git reset --soft HEAD~1

Remove the last commit and discard all changes:

git reset --hard HEAD~1

Official git-reset documentation.

Remove files which are not needed any more

Use the -n option to show you: what would I do when cleaning… . This helps you to prevent deleting files that shouldn’t be.

git clean -n
git clean

The -f forces the deletion of the files.

git clean -f

Official git-clean* documentation.

Create or delete branches

Create Branches

Create a new local branch:

git branch <branchName>

Delete Branches

Delete a local branch:

git branch -d <localBranchName>

Delete a remote branch, but be sure to know what you are doing.:

git push origin :<remoteBranchName>

or

 git branch -d -r origin/<branchName>

Remote Repositories – Fetching, Pulling, Merging

Get the remote origin URLs

If your local repo ist connected to a remote repo you can check for the URLs of the remote origins with the following command. The -v stands for verbose and makes the command show the URLs:

git remote -v

Show all remote branches

git branch -r

Add a remote repository

git remote add <somename> <http://git....>

Fetch the changes from the remote repository

git fetch

If you have local references of former origin branches you can remove the references locally with fetch -p (–prune):

git fetch -p

Merging branches

Merge from a remote branch to the local branch. This could happen with „fast forward“ which means: was able to add the changes without modifying anything.

git merge origin/master

Official git-merge* documentation.

Pulling from remote origins

Pulling, is actually a shortcut to fetch and merge (if the branch is synchronized with a remote repository)

git pull

For local branches, it is better to use a –rebase. This keeps your commit history clean so the local changes are not „merged“. (Be aware: Using a –rebase with remote origins is not a good practice because you change the commit ordering of other people).

git pull --rebase

Pull from a branch:

git pull origin master

Tagging

Tagging is used to find special commits, meaning you can tag a special commit for example to define a version 1.0 of your software. You can then always find the tag later to start from there.

Lists Tags to tag versions:

git tag

Tag the current state to a tag:

git tag V1.0

Tag the current state to a tag with a message:

git tag -a V1.0_withmessage

 

Official git-tag* documentation

Tags must be pushed to git, to be available in the origin repository

git push --tags

.gitignore

Common mistakes

A common mistake that I experienced when creating the .gitignore file is the handling of sub-folders:

subfolder/ <– ignore a sub-folder that is located anywhere in the source tree anywhere
/subfolder <– ignore a sub-folder located in the root of the repository

.gitignore file is not working

My ignorefile was not working. It was not recognized and all changes had no effect.

The following line did the trick. It deletes the local cache. After that I had to commit all changes and it works.

git rm -r . --cached

* All Links marked with an * link to an external page.

Schreibe einen Kommentar