Jump to content

FAQ: Solutions to common GIT problems


Recommended Posts

As there are a lot issues that people think cannot be solved in any good way, this thread can serve as a discussion about those problems. Feel free to propose a problem that you don't see any good solution to and when (not if) someone comes up with a solution it can be moved to the first post in the form of a FAQ.

What are the most common operations in Git and what do they do ?

Clone is for downloading a fresh repo.

Fetch is downloading changes without applying them.

Merge is merging changes that you've downloaded with existing changes/commits

Checkout is switching the source to a specific version (local branch or commit) of it that i've downloaded

Reset leaves you on the current branch but changes what the branch "points to".

Pull is a shortcut for fetch + merge.

Commit is a commit, but always local.

Push is sending your local commits to a remote location.

How do you view the changelog ?

Use the "gitk" command or go to http://github.com/mangos/mangos/commits

Gitk only shows the changes on my local branch so how do I view the changes on another ?

gitk origin/303-willcrashforsure

How do you view only the changes to a particular file ?

gitk src/shared/revision_nr.h

How do you apply new changes from a remote source (or "update my sources to the latest revision") ?

In git, "updating to a new rev" is pulling the changes from origin (the location you cloned from). The command for this is "git pull origin". You can also pull changes from other locations, for example one of the many interesting git branches. Pull means download the changes and merge them with the commits on your local branch so you can merge as many branches as you want, but you may get conflicts and you will have to resolve them.

How do you apply a patch ?

You can do either "git apply path/to/patch" or if that doesn't work, "patch -d . -p0 < path/to/patch". After one of those, it's recommended that you commit the change locally so if you apply multiple patches you can see them clearly and because merge tools work best when the sources have no uncommitted changes. So do it with: git commit -a -m "Added patch X"

How do you update when you have uncommitted changes to your sources ?

You can either commit the changes (recommended, read above) or do the following:

- "git stash" removes all your uncommitted changes and saves them for later

- update to new sources

- "git stash apply" restores all your uncommitted changes

How do you make a patch / view the diff from all the changes that you've done, committed locally or not ?

- view all of the differences between the state of your local branch and that of the remote master branch

git diff origin/master

- save them to a file as a patch

git diff origin/master > file.patch

How do you view the contents of a file, as it was in a particular commit ?

git cat-file blob HEAD^^:src/shared/revision_nr.h <- two commits before the current HEAD (tip of the local branch)

git cat-file blob 93519a34be107b1bd36f2d2fe8af3c03f42bf1f9:src/shared/revision_nr.h <- at a particular commit

git cat-file blob origin/303-willcrashforsure:src/shared/revision_nr.h <- in a different branch

Link to comment
Share on other sites

  • 38 years later...

How to resolve merge conflicts ?

You can do it with git diff by searching for the conflicts and editing them but there are easier ways to do it. If the "git mergetool" command is called after a failed merge, based on how it's configured it will start one of many tools to resolve the conflicts. I'll present some of the available tools, how to install, configure and use them. You can try one of the following and see which one works best for you:

1. KDiff3 (not kdiff or kdiff2) - http://kdiff3.sourceforge.net/doc/merging.html

- has a fairly good visual interface and works on both windows and linux

- you can get it from http://sourceforge.net/project/showf...ease_id=501369

- to make sure git can find it, for example on windows if you installed it to "c:/Program Files/KDiff3"

git config mergetool.kdiff3.path "c:/Program Files/KDiff3/kdiff3.exe" <- either that or you could add c:/Program Files/KDiff3 to your path

- make it the default mergetool

git config merge.tool kdiff3

2. vimdiff

- opens a text mode GUI with a three way split

- haven't tried it on windows, but should be availabe on most linuxes

- this is the default merge tool if it's installed

3. Perforce Merge http://www.perforce.com/perforce/products/merge.html

- good visual interface, has both windows and linux versions

- download the perforce visual client from http://www.perforce.com/perforce/downloads/index.html

- install it

- add it as custom merge tool (if you installed it to c:/Program Files/Perforce/)

git config mergetool.p4.cmd ' "c:/Program Files/Perforce/p4merge" "$BASE" "$REMOTE" "$LOCAL" "$MERGED" '

- make it the default mergetool

git config merge.tool p4

4. Tortoise Merge http://tortoisesvn.tigris.org/TortoiseMerge.html

- good visual interface, only works on windows

- download and install TortoiseSVN from http://tortoisesvn.net/downloads

- in "C:\\Program Files\\TortoiseSVN\\bin" create a file named TortMer.bat and add the following:

@ECHO OFF

TortoiseMerge.exe /base:"%PWD%/%1" /theirs:"%PWD%/%2" /mine:"%PWD%/%3" /merged:"%PWD%/%4"

- add a custom merge tool to git that runs this batch file

git config mergetool.tortoise.cmd ' "cmd.exe" "/CTortMer.bat" "$BASE" "$REMOTE" "$LOCAL" "$MERGED" '

- make it the default mergetool

git config merge.tool tortoise

5. Beyond Compare 3 http://www.scootersoftware.com/moreinfo.php?zz=moreinfo_merge

- good visual interface, has both windows and linux versions, costs money if you want more than a trial version

- download and install BC3 from http://www.scootersoftware.com/download.php

- add it as custom merge tool (if you installed it to c:/Program Files/Beyond Compare 3/)

git config mergetool.bcomp.cmd ' "c:/Program Files/Beyond Compare 3/bcomp.exe" "$LOCAL" "$REMOTE" "$BASE" "$MERGED" '

- make it the default mergetool

git config merge.tool bcomp

Link to comment
Share on other sites

  • 1 month later...

How to use two working copies at the same time without downloading the changes twice ?

On linux it's fairly simple. There is a tool called git-new-workdir. In effect, you can fetch from one directory and use it in the other, you can create a local branch in one directory and switch to it on the other, you can make local commits in one directory and they will appear in the other, you can work on two things at the same time without having to constantly rebuild when you switch to the other one, you save hard disk space and time by not downloading the same thing twice.

On windows XP, this tool doesn't work because it uses symbolic links (which are only available in Vista, and even there ln -s may not work). The same effect can however be obtained on XP if you use NTFS. It doesn't have symlinks, but it does have hard links and junctions which combined work in almost the same way. To be able to use these you need some shell extensions. After you've installed those you get a "Copy-Paste like" interface in the context menu. By right clicking it, you can pick a file or a directory as a link source and then, in another location you can drop either a hardlink to that file or a junction point to that folder, by selecting it from the context menu (right click). With this tool we can basicly do the same thing that git-new-workdir does (if old_workdir is the folder where the repository we want to copy was cloned):

- create the folders new_workdir, new_workdir/.git, new_workdir/.git/logs

- copy the file old_workdir/.git/HEAD to new_workdir/.git/HEAD

- make junctions in new_workdir/.git to the hooks, info, objects, refs folders from old_workdir/.git

- make a junction in new_workdir/.git/logs to the refs folder from old_workdir/.git/logs

- make hard links in new_workdir/.git to the config, packed-refs and (if they exist) the remotes, rr-cache, svn files from old_workdir/.git

- in new_workdir run: git checkout -f master <- or any other local branch, preferably not the same branch you're using with the other workdir

- done!

Notes:

- the only bug I've found so far is that sometimes the packed-refs file seems to revert to a normal file, not a hard link resulting in changes to local branches not being seen in the other repo, but at least fetched objects are still common. So far I've fixed this by deleting the file and making the hard link again, but it may not be the appropriate action if you want to preserve some changes done to those refs.

- if you have the same local branch checked out in both workdirs, when you make a local commit, the branch on the other workdir will be affected and it will look like you reverted the changes on the other workdir.

Link to comment
Share on other sites

  • 5 months later...

I'll post it here and update from time to time

"5"

WARNING: Those commands in the current form won't work always (for experienced users with cutomized repository) and can be used in a more powerful way, but they should work fine for those whom this FAQ is intended.

-------------------------------------------------------------

"4"

git reset --hard 123456789

where 123456789 is a commit hash. You can get the hash on http://github.com/mangos/mangos/commits/master or http://repo.or.cz/w/getmangos.git?a=heads by clicking at the commit and looking on the URL for example.

-------------------------------------------------------------

"4"

In most cases no, delete everything EXCEPT the .git directory and then run

git reset --hard origin/master

(or replace master with the branch you want).

-------------------------------------------------------------

"4"

Run

git gc

once a month.

-------------------------------------------------------------

TODO HERE

Link to comment
Share on other sites

  • 4 months later...
I'll fix the MAX_PATCHES one before the end of the night. I don't know much about the "-chop, -chopcurve, -chopsky" settings, but between the scaling of textures and detailing of certain objects, you should be able to fix the error with out those settings.

Did I miss any other common errors?

Pretty much, yea. Other than that, you look like another nice I-wanna-be-smart spambot.

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use