Skip to content

Instantly share code, notes, and snippets.

@arwer13
Last active May 11, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arwer13/618a934f65d7cd9a34e21d3bd1859fef to your computer and use it in GitHub Desktop.
Save arwer13/618a934f65d7cd9a34e21d3bd1859fef to your computer and use it in GitHub Desktop.

The git questions

  1. Can you explain git rebase, git checkout, and git merge, and give examples of when I should use them?
  2. How does git diff work and how can I resolve merge/rebase conflicts from the command line?
  3. What do remote, origin, and HEAD mean?

The git asnwers

Remotes, origin, HEAD

Git has the concept of "remotes", which are simply URLs to other copies of your repository. A remote repository is just a remote's local repository, almost like the one of yours, and which you can access by, for example, ssh.

When you clone another repository, git automatically creates a remote named origin and points to it. So origin is an alias on your system for a particular remote repository. You can see more info about the remote by typing git remote show origin

By doing

git push origin branchname

you're saying to push to the origin repository.

HEAD is a reference which points either to the last commit of current branch or to some arbitrary commit. And under the hood HEAD is just a text file with the reference in it. One may inspect it by cat .git/HEAD. In the second case, when HEAD points to a commit that is not the last commit in a branch, that is called a "detached HEAD". In other words, HEAD is a reference to some state of your files saved in git.

Checkout

The checkout is used either to switch to another (or new) branch or to update files in your working tree to some of their git state. To switch to branch one uses

  • git checkout <branch> to switch to specified branch
  • git checkout -b <branch> to creatе specified branch and switch to it

To update indexed but unstaged files to thier HEAD state:

  • git checkout -- . or git checkout . to update all such files (do the same, just different syntax (more detailed here))
  • git checkout <path to file> to update specific file

Their is is self-explanatory)

Merge and rebase

Merge and rebase are used to unite changes from multiple branches. On the difference between them

In collaborative work team usually arranges to use one of them to make commit history look uniformly.

More common and easier way is to use merge.

Here is on common situation when one uses merge or rebase. Assume there is a more or less stable branch develop. Some people use it and suppose it contains some working version. And there is some big feature to be implemented, which wouldn't fit into one commit. Then developer should create a branch for it, let it be feature-XXX. Work there till feature looks good, and merge it into develop (or rebase onto develop). To do so he

git checkout develop
git merge feature-XXX

Git diff

As documentation says git diff may do quite a lot of comparison

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk.

depending on it's arguments.

I do not use it at all. If there are more than few lines changed the output is very messy, so I use some GUI wrappers: GitExtensions on Windows and git gui or gitk on Linux.

But here are some common usages:

  • git diff for not staged changes in the working tree
  • git diff HEAD for staged and not staged changes in the working tree
  • git diff b1 b2 for changes between the tips (last commits) of b1 and b2 branches
  • git diff HEAD^ HEAD for changes between the version before the last commit and the last commit
  • git diff HEAD^ HEAD for changes between the version before the last commit and the last commit

Merging conflicts

Here is a short and clear article on how to resolve conflicts from command line. I think it doesn't make sense for me trying to compress it.

Links used:

SO: what-is-origin-in-git

SO: what-is-head-in-git

Git docs: git-checkout

Git docs: git-diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment