Git Basics655 2018-10-20 15:28
Git is a version control system we used to manage source code. It features a graph history of commits, so that people can easily rollback to previous versions and figure out who authored what.
When host the project with git on a remote server, you get a remote repository that a group of people can collaborate with.
Apart from the remote repository, people usually want to manage work items online with an issue tracking system.
GitHub = remote git repository + issue tracking system. In the following steps, let’s assume we are using Github to collaborate.
git config --global user.name "Your Name Here" git config --global user.email "[email protected]"
git clone git://github.com/bar/foo.git
View commit history.
git log --graph
However, I highly recommend gitk for visualization, instead of sourcetree or gitkraken.
# view current branch gitk # view all branches gitk --all # view a specific commit gitk <hash>
Check the status often because many hints come from there.
Many settings are stored under
.git/. It helps a lot to view those files.
# Only work on your own branch # Create the branch (often with the name of the issue's id number) git checkout -b workingBranch # now on your workingBranch, do your work, and then commit git add . git commit -m "commit messages" # or without `-m` to edit message in vim # submit to code review git push origin workingBranch:workingBranch # then go to github to submit a pull request # do changes according to code review, and then commit git add . git commit --amend # re-commit # rebase your changes on the latest master # switch to master branch git checkout master # pull the latest code from origin/master git pull origin master # Go back to the working branch git checkout workingBranch # now on your workingBranch, rebase on master git rebase master # if there are conflicts, resolve them, (rebuild your proj) and then you # need to continue. git rebase --continue # now your changes are on the latest master, and then the master should # merge the workingBranch git checkout master git merge workingBranch # If there are `fast-forward` keyword, good # re-build your proj to ensure no breaks git push origin master # Till now, you are on the local master = origin master + your own commit # awaiting to be pushed. # If at this time, unfortunately, someone else pushed after your last # pull. You have to re-do the rebase stey by: git checkout workingBranch # delete the master git branch -D master # get the up-to-date master from origin/master git checkout -b master origin/master git pull git checkout workingBranch git rebase master git checkout master git merge workingBranch git push origin master # If succeed, delete the workingBranch git branch -d workingBranch
Undo the latest commit message but the modifications to the code remain.
git reset --soft HEAD^ # ^ means the one before HEAD
Undo the latest commit completely, including the modifications.
git reset --hard HEAD^
What remote repository the ‘origin’ corresponds to?
git remote -v
How to create a new local branch foo?
git checkout -b foo
Verify publickey settings on Github?
ssh -T [email protected]
How to create upstream branch?
git remote add upstream git://github.com/octocat/Spoon-Knife.gitAssigns
Create a repo
mkdir projectFolderName cd projectFolderName git init Sets up the necessary Git files
Push local branch to remote master branch
git push https://github.com/puncsky/WebFx.git 301:301
Clean local directory
git clean -d -x -f # will remove untracked files, including directories (-d) and files ignored by git (-x). Replace the -f argument with -n to perform a dry-run or -i for interactive mode and it will tell you what will be removed.
There are many other commands very helpful like
git cherry-pick not covered in this doc.
If you find this article helpful