- Can you explain git rebase, git checkout, and git merge, and give examples of when I should use them?
- How does git diff work and how can I resolve merge/rebase conflicts from the command line?
- What do remote, origin, and HEAD mean?
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.
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 branchgit checkout -b <branch>
to creatе specified branch and switch to it
To update indexed but unstaged files to thier HEAD
state:
git checkout -- .
orgit 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 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
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 treegit diff HEAD
for staged and not staged changes in the working treegit diff b1 b2
for changes between the tips (last commits) ofb1
andb2
branchesgit diff HEAD^ HEAD
for changes between the version before the last commit and the last commitgit diff HEAD^ HEAD
for changes between the version before the last commit and the last commit
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.