Skip to content

Instantly share code, notes, and snippets.

@casouri
Last active January 1, 2018 05:27
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 casouri/12e118459b8909d9cf4ea8de8ab3ffae to your computer and use it in GitHub Desktop.
Save casouri/12e118459b8909d9cf4ea8de8ab3ffae to your computer and use it in GitHub Desktop.
git tutorial - pulling everything together

Git tutorial - pulling everything together

So you have know something about git: you have create a repository on GitHub, cloned it to your computer, coded a bit and committed commits. Maybe even you have tried to create a new branch and merged it back. Everything looks so nice, until you dug a litter deeper. Where do you push to and pull from? What is fork? What about “pull request”? What is it’s relationship to pull? what is all this mess? What does checkout checkout? What is HEAD? My HEAD is hurting!

It is actually not a mess. You will be able to pull everything together.


First GitHub is “distributed”, means everybody have a full copy of the repository, with their own branches.

pull

When you want to copy code from someone, you pull her repository, and you normally want to specify a branch. pull is similar to merging a local branch.

push

push is not opposite of pull. When you have a repository on GitHub and cloned a local repository, committing in your local repository doesn’t affect the repository on GitHub. To “sync” to directories, you need to “push” your commits(and branches and merges etc) to the repository on GitHub.

What is origin master?

When you see others using push and pull, origin master appears a lot. What is it really?

master is master branch, a branch named master. What makes it special is that this is the “default branch”, or the “main” branch. You start with this branch when you create a repository. And normally all your dev, bug-fix, new-feature branch will merge back to master.

origin is a remote, remotes are other repositories (possibly owned by other people) of the same project. When you clone a repository from GitHub. A remote named “origin” will be automatically created and point to the url of that GitHub repository. So, push origin master will push to master branch of the GitHub repository you cloned from.

Since you know remote, we can look back to pull. In fact, pull is fetch and merge. fetch fetch everything from a remote.

What is “fork” and “pull request”?

When you create a repository on GitHub, only you can modify it. Other people cannot just clone your repository and push to it. In order for other people to contribute to your project, they can “fork” your repository to their account, then clone it. So they can modify and push to it. When they think the code is good and want to merge it to your code, they can create a pull request from their code to your code. Then you can review, comment, accept, cherry pick or reject their code.

What is “upstream”?

Sometimes you forked others code and modified it to your own likeness. Then origin author made a update. You would want to pull from that repository to keep your version up to date. So you add another remote named “upstream” which points to her repository Then you can pull upstream master

What is HEAD?

I as a beginner myself don’t really understand all about HEAD. But in a nutshell, it is a pointer on which all your actions take effect. When you move HEAD, it also brings your stage and working directory with it.

For example, if you move your HEAD from master branch to dev branch, all your commit will commit to dev branch. merge bug-fix will merge bug-fix branch to dev1 branch.

In a word any work on history will take effect on where HEAD is.

Great, then how to move it? This question leads to next section.

What is =checkout=?

checkout moves HEAD to some branch.

In order to move HEAD to some remote’s branch, first fetch then checkout.

P.S. HEAD automatically moves along commit and merge.

A summary

https://casouri.github.io/img/git-git.png

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