Git

Notes from http://gitref.org/remotes/
 
If you want to share a locally created repository, or you want to take contributions from someone else's repository - if you want to interact in any way with a new repository, it's generally easiest to add it as a remote. You do that by running git remote add [alias] [url]. That adds [url] under a local remote named [alias].
git remote add  asf_mesos https://github.com/apache/mesos.git
 
git remote rename [old-alias] [new-alias] rename remote aliases
If you want to rename remote aliases without having to delete them and add them again you can do that by running git remote rename [old-alias] [new-alias]. This will allow you to modify the current name of the remote.
 
git remote set-url update an existing remote URL
Should you ever need to update a remote's URL, you can do so with the git remote set-url command.
 
git fetch download new branches and data from a remote repository

 git pull fetch from a remote repo and try to merge into the current branch

The second command that will fetch down new data from a remote server is git pull. This command will basically run a git fetch immediately followed by a git merge of the branch on that remote that is tracked by whatever branch you are currently in. Running the fetch and merge commands separately involves less magic and less problems, but if you like the idea of pull, you can read about it in more detail in the official docs.

Assuming you have a remote all set up and you want to pull in updates, you would first run git fetch [alias] to tell Git to fetch down all the data it has that you do not, then you would run git merge [alias]/[branch] to merge into your current branch anything new you see on the server (like if someone else has pushed in the meantime). So, if you were working on a Hello World project with severl other people and wanted to bring in any changes that had been pushed since we last connected, we would do something like this:

 

You can see the mapping that Git makes. The 'master' branch on the remote repository becomes a branch named 'github/master' locally. That way you can merge the 'master' branch on that remote into the local 'master' branch by running git merge github/master. Or, you can see what new commits are on that branch by running git log github/master ^master. If your remote is named 'origin' it would be origin/master instead. Almost any command you would run using local branches you can use remote branches with too.

 

git diff

Finally, to see the absolute changes between any two commit snapshots, you can use the git diff command. This is largely used in two main situations - seeing how two branches differ from one another and seeing what has changed since a release or some other older point in history. Let's look at both of these situations.

 

 

That's what we're looking for, but we don't want to have to figure out what commit the two branches diverged from every time. Luckily, Git has a shortcut for this. If you run git diff master...erlang (with three dots in between the branch names), Git will automatically figure out what the common commit (otherwise known as the "merge base") of the two commit is and do the diff off of that.

$ git diff --stat master erlang

 

git diff --stat master...erlang

 

 

Nearly every time you want to compare two branches, you'll want to use the triple-dot syntax, because it will almost always give you what you want.

As a bit of an aside, you can also have Git manually calculate what the merge-base (first common ancestor commit) of any two commits would be with the git merge-base command:

$ git merge-base master erlang 8d585ea6faf99facd39b55d6f6a3b3f481ad0d3d

You can do the equivalent of git diff master...erlang by running this:

$ git diff --stat $(git merge-base master erlang) erlang

 

 

 

git push (remote-name) :(branchname) delete a remote branch

In the above example you've deleted the "tidy-cutlery" branch of the "origin" remote. A way to remember this is to think of the git push remote-name local-branch:remote-branch syntax. This states that you want to push your local branch to match that of the remote. When you remove the local-branch portion you're now matching nothing to the remote, effectively telling the remote branch to become nothing.

git merge merge a branch context into your current one

Once you have work isolated in a branch, you will eventually want to incorporate it into your main branch. You can merge any branch into your current branch with the git merge command. Let's take as a simple example the 'removals' branch from above. If we create a branch and remove files in it and commit our removals to that branch, it is isolated from our main ('master', in this case) branch. To include those deletions in your 'master' branch, you can just merge in the 'removals' branch.

 

 
posted @ 2016-06-04 11:28  Andy.gbhu  阅读(281)  评论(0编辑  收藏  举报