Skip to content

Git Tips and Tricks

Martin Andersen edited this page Nov 7, 2013 · 4 revisions

Basics

Git is fairly logical once you have learned the ideas behind it, so there is quite a lot here on concepts, and much more abbreviated notes on the important commands.

The following site contains a basic introduction to Git version control: http://www.stuartellis.eu/articles/git-basics/

However, the real deal exists over at (see chapters 1, 3 and 5): http://git-scm.com/book

Learn Git Branching

Tips and tricks

The following section contains various tips and tricks which can come in handy when working with Git.

#1 Remembering your username and password:

In order to store the username/password, use the following command: git config credential.helper store

Note: A file containing your login details will be stored in raw text on your machine(Defaults to ~/.git-credentials)!

If you want Git to resume to asking you for credentials every time it needs to connect to the remote repository, you can run this command: git config --unset credential.helper

#2 Moving files

At some point in time, you may want to move a file from one folder to another. To do so, there are a few steps needed in the process.

In order to move a file from one place to another, simply use the mv command from the terminale, like so:

mv <source_path> <target_path>

where the <source_path> and <target_path> are replaced by the actual paths. Once the files are moved, git will detect that the file has been moved. However, it can be a bit tricky to infer this from doing a simple git status command in the terminal. Once the file(s) are moved, git will say that a new file has been created, and the old one been deleted. This is simply a way of saying that the files was moved, so when you track the changes from both the added and deleted file, you should see that the file is now renamed, meaning that the file move was succesful. If you do not see the renamed change, something went wrong (see note below)!

Important: When moving files, it's very important that you do not edit the content [1] of the file before or after the files was moved, before you do a commit. Otherwise, the history will be erased!

When committed, the full history of the file moved can be viewed using the command: git log --follow <file_path>

#3 Revert the last commit and remove it from the history

Suppose you end up committing something locally that you end up regret, how can you remove this commit from your history, and revert back to the previous commit?

If you don't care about the commit, just do: git reset --hard HEAD~ This will blow away the commit.

If you want the changes to be in working directory, do: git reset HEAD~

#3.1 Force git to clear local branch and fetch remote I had some problems while using Google Drive for my local branch at home. It went out of sync with the remote, after the files were synchronized. used:

git fetch --all git reset --hard origin/master

to fetch and overwrite my local files.

#4 Handling merge conflicts

(This article is not done...)

When handling a merge conflict, it is important that you don't commit your merge branch before verifying that it actually compiles.

#6 How do I edit an incorrect commit message in Git?

git commit --amend -m "New commit message"

#7 Restoring a deleted file (see [2])

  1. Use git log --diff-filter=D --summary to get all the commits which have deleted files and the files deleted.

  2. Use git checkout $commit1 filename to restore the deleted file. Here $commit1 referes to:

  • $commit: the name of the commit you want to start from.
  • ~X: allows you to specify X commits before the specified commit, so ~1 is the commit before, ~2 is two commits before, etc. Not specifying X will implicit refer to ~1.

Example: git checkout HEAD~ filename - this will go back to the previous branch from the local HEAD

#8 Add forgotten changes to the last git commit

See: http://www.commandlinefu.com/commands/view/1249/add-forgotten-changes-to-the-last-git-commit

#9 Change default editor for commit messages to Note++

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

#10 View history for a file

With Git bash: gitk --all relativ path to file. Everthing is case sentitiv. Example : gitk --all eTray/Propertydesigner/Propertydesigner/Repository/FakesForPropertiesRepository.cs

With gitextensions: Install gitextensions from here https://code.google.com/p/gitextensions/ when finish you can right-click on a file in file explore and under the menu "git extension" select "File History" and you get a nice UI.

#11 Create a new branch from the code modified in the master branch

http://stackoverflow.com/questions/1453129/git-create-branch-from-current-checked-out-master

#11 Roolback one file

git checkout HEAD -- filename

#12 Mergning single commits into branch

http://blog.katipo.co.nz/2008/09/01/merging-in-single-commits-with-git/

#13 How to Push a New Local Branch to a Remote Git Repository

http://www.mariopareja.com/blog/archive/2010/01/11/how-to-push-a-new-local-branch-to-a-remote.aspx

#14 Set default editor for commit messages to use Sublime $ git config --global core.editor "C:\Program Files\Sublime Text 3\sublime_text.exe"

References

[1] http://stackoverflow.com/questions/10828267/can-git-restructure-my-folders-without-losing-history [2] http://verboselogging.com/2010/10/19/restore-deleted-files-in-git

Clone this wiki locally