The is the very bare-bones basics of git.
First, I am staring by assuming that someone else has already setup a git repository on github. If it is a private repository, the author must grant you access to that repository by adding your github account name to the project.
Once this is done, you will see this project under “Your repositories.”
Every git project has a git URL associated with it. You will find this URL on the project’s page. It looks something like this:
In this example “johnsmith” is the author’s github account and mygreatproject is the name of the project. You will find this URL in the Github interface – you can copy and paste it right from there. In fact, Github even provides a handy “Copy to clipboard” button just to the right of the URL in its interface.
With this, you execute git clone plus the Git URL to clone the repository to the current working directory.
Git will create a directory with the same name as the repository in your working directory and clone the repository into there. Next cd to that directory to start working with that project.
Branching. In all of the following examples, note that “xyz” is the name of your branch.
|git checkout master||first switch to the master branch|
|git push origin HEAD:refs/heads/xyz||makes a new remote branch called “xyz”|
|git branch -a||see what you just did. You should see master in green and (at least) two in red:
|git checkout -b xyz --track origin/xyz||Makes a new local branch xyz and tells git to push this branch back to the repository branch origin/xyz. You will see this result:
Branch xyz set up to track remote branch refs/remotes/origin/xyz.
Switched to a new branch “xyz”
|git branch -a||Now you should see both your master and your branch locally and on the remote repo.
If you mess up, you can delete a branch like this
|git branch -D xyz||delete the xyz local branch|
|git push origin :heads/xyz||delete the xyz branch on the REMOTE server|
Switching Between Branches
|git checkout mybranch||switches branches to “mybranch”|
|git checkout master||switch back to master branch|
|git checkout -b xyz --track origin/xyz||switches to the xyz and tells git to push the branch back to the origin|
Committing, Pushing & Pulling
|git status||shows what you’ve changed|
|git add .||add everything that’s been changed in current folder|
|git diff HEAD | mate||show how my current version differs from the the HEAD|
|git commit -v||commits my changes to my local repo (-v shows diff) (will open commit log, enter message at top of screen, then save & doc)|
|git push||pushes all branhces|
|git push origin mybranch||push mybranch to the “origin” server|
|git pull --rebase origin mybranch||pull others’ changes by doing a rollback first, then applying your changes on top of what’s been changed since the last time you pulled|
|git stash||(this will “stash” your changes away so you can pull)|
|git pull --rebase origin mybranch|
If you get a conflict, fix the conflict (by editing the conflicted files and choose if you want to take the head’s version or your version), then
|git add .||adds the conflicted file back to the index|
|git rebase --continue||continues the rebase option|
|git push||Since we already have a commit created by the rebase, we don’t need another commit|
|git stash apply||Applies the changes we stashed away before|
|then try committing & pushing and you shouldn’t get a conflict.|
|git log||see log recent commits|
||apply a specific commit to whatever branch you are in after you cherry-pick, do git push.
|git reset --hard||this will throw away changes you’ve made in your working branch|
This assumes that mybranch is branched from mystable. You are going to take any changes made to mystable since the time you branched onto the branch.
|git checkout mybranch||switch to mybranch branch if not already there|
|git merge mystable||merge any work done on mystable INTO mybranch|
|git status||to see what has conflicted|
|IF you have any conflicts, resolve your conflicts manually|
|git add .||(either add all or just the ones that conflicted)|
|git commit -v||put resolutions & dev changes into a commit. Do not do another commit if you are not resolving any conflicts.|
|git push||Finally, put this back to your branch|
Color coding git:
Going back in time.
git checkout – .
now you’re back at that commit
then to move forward again git pull
git checkout master
git checkout some-other-branch
git rebase master
if there are conflicts, you’ll need to resolve them then do
git add .
git rebase --continue
once everything is all resolved, you need to push the newly-rebased branch up to github with a --force
git push origin some-other-branch --force