Git Cheatsheet


git clone <path or url to repo>

Create an empty repo

git init

Check if upstream has updates

git fetch

Create a branch

git branch mybranch

Switch to another branch

git checkout mybranch

Create and switch to another branch in 1 command

git checkout -b mybranch

Pull upstream updates

git pull

Add a file

git add filename

Move a file

git mv sourcefile destinationfile

Note: You can move a directory or source file or destination file can include directories.

Delete a local branch

git branch -d mybranch


git status

Revert uncommitted changes to a file

git checkout path\to\file.ext

Remove all untracked files

This makes the repository clean again.
Do a dry run first with -n.

git clean -n

Then do it for real with -f.

git clean -fxd

git diff

git diff

git merge

git merge myBranch

Take all upstream source files

git checkout --ours .
git add .

Keep all local files

git checkout --theirs .
git add .

Abort the merge

git merge --abort

git reset or undoing a local commit

Reset your local branch to head, but keep all the changes. Use this to undo a commit.

git reset HEAD^

git rebase

This at first looks easy. But there is complexities, especially if you have already pushed.

git rebase master

If a merge conflict occurs, fix it and then run:

git rebase --continue

If you have already pushed, run this to push once rebase is complete.

git push --force-with-lease

git squash all commits

This is a multistep process. The assumption is that you are in your feature branch.
Make sure you have no lingering changes and everything is committed before starting.
Branch name in example: FeatureA

git checkout master
git pull
git checkout -b FeatureA_2
git merge --squash FeatureA

Now if you want the branch named the same, you can delete FeatureA and rename FeatureA_2 to FeatureA.

Delete local branch

git branch -d yourbranch

To force, just use a capital D.

git branch -d yourbranch

Rename local branch

git branch -m newBranchName

If you are in master and want to rename a feature branch without checking it out:

git branch -m oldBranchName newBranchName

Git conflict with Visual Studio .sln file

Often, when multiple developers are working on the same solution and adding new projects to it, git will conflict easily.
Instead of trying to merge the .sln, it is often much faster, especially if you have only added a project or two, to just take the current master’s .sln and re-add your projects to the sln.

So imagine you are working on branch FeatureA.
Note: Remember, where “ours” and “theirs” points to is opposite of where they point to on a merge.

git checkout master
git pull
git checkout FeatureA
git rebase master
git checkout --theirs /path/to/yourproj.sln
git rebase --continue

You will then have to save your commit as the commit text will open. Remember to press “Esc + Shift + :” and then type wq! and hit enter.
Now, if your branch has many check-ns, you may have to repeat the process to keep the master (theirs) .sln file.

Once your rebase is completed, make your changes to your .sln and check them in.

Change the git editor from ‘vim’ to ‘notepad++’

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

Leave a Reply

How to post code in comments?