git config user.email "abc@abc.com" //same to the user.name
Commands
Git command syntax
1 2 3 4
git [switches] <command> [<args>]
git --version // --version is a switch git -p config --global user.name "abc" //-p is a switch
Git process
1 2 3 4 5 6 7 8 9 10
git init git status
git add display.html git commit -m "Adding the display.html" git status git log git log --stat // --stat, to see the files that are part of a commit
git ls-files
To update and change files
1
git commit -a -m "Adding one line in display.html"
equivalent to
1 2 3
git add "display.html" git commit -m "Adding one line in display.html" git commit --amend // you can amend your commit.
The -a switch to git commit says to automatically stage (run git add) any files that Git knows about. The reason we still need to “add” the file is the STAGING feature of Git. git add pushes the file to a staged area, but not yet commit. The version in the staging area is what will be committed when you run git commit.
git add does take a directory name that will add all the files in that directory (including the untracked ones).
1
git add --dry-run .
“dot” for the current directory. It does a dry run, showing you what it would have done, NOTE, a preview of what is going to happen.
To view the difference
1
git diff
The copy of the file in the working directory vs. the copy of the file in the staging area.
1
git diff --staged
The copy of files that you committed earlier vs. the version of the file that you staged(via git add)
1
git diff master...dev
It checks the difference between two branches. The order of branches to the git diff command is significant, master is listed first, and bugfix is listed second. Remembering the highway analogy, bugfix (the on-ramp) will merge onto master (the highway).
1
git diff --name-status master...bugfix // Display the the information of the files being merged, should be useful while multiple files are merging
To view the git history
1 2 3 4 5 6 7 8
git log --oneline // --oneline a compact listing of the Git output log. git log --shortstat --oneline git log --parents --abbrev-commit git log --patch git log --stat git log --patch-with-stat git log --oneline math.sh //This lists all the commits that touch math.sh git log --graph --decorate --pretty=oneline --all --abbrev-commit
To remove the file from the staging area of Git
1
git rm"toRemove.txt"
Shortcut to remove “toRemove.txt” from the staging area as well as the working directory. Or you can remove it from the working directory first, then remove it from the repository.
To rename the file
1
git mv
To add parts change of a part, using
1
git add -p
Then it prompts a command line editor.
Reset Command
1
git reset math.sh
The edits that you added to the staging area are removed. More important, the working directory is untouched
Git checkout
1
git checkout -- math.sh
When you check out a file in this manner, you overwrite the file in the working directory
1
git checkout --patch dev FaceDetector.java
Check out files from another branch
1 2
git checkout abcdefg // go back to the commit abcdefg, time travel back git checkout master // go back to present
Git rev-parse
1 2
git rev-parse HEAD git rev-parse master
Both commands produce the same output: the SHA1 ID of the latest commit.
Git Tag
You normally use git tag immediately after git commit.
1 2 3
git tag // show all tags git tag first_commit_trial -m "The first commit" YOUR_SHA1ID // To add tag to the specific commit YOUR_SHA1ID. git show first_commit_trial // Display the information about the tag "fist_commit_trial".
Git Branchs
1 2 3 4 5 6 7
git branch // list all branches git branch dev // create a new branch, dev git checkout dev // check out means changing the working directory to reflect the contents of the branch git branch -d master // -d switch to delete the master branch. git branch -v //The -v switch displays the SHA1 ID of the tips of these branches. git branch fixing_readme YOUR_SHA1ID // Create a branch based on the YOUR_SHA1ID
To switch branches.
1 2
git checkout fixing_readme // Switch to the fixing_readme branch git checkout -b another_fix_branch fixing_readme //Using git checkout’s -b switch, you can create a branch and check it out in one step.
To delete and re-create branches.
1 2 3 4 5
git branch -d another_fix_branch Deleted branch another_fix_branch (was d6cc762). //if you want to re-create the branch, just typing: git checkout -b another_fix_branch d6cc762 //confirming the branch is back by typing: git log //If for some reason you don’t have the SHA1 ID, you can resort to git reflog. git reflog
Git Stash
Git does have a facility for putting aside your work temporarily: the git stash command. You can use this to save all your work temporarily, leaving you with a clean working directory.
1 2 3
git stash git stash list // List works in progress that you’ve stashed away. git stash pop //To reapply this work to your current branch.
Git Merge
1 2 3 4 5 6 7
git checkout master // checkout master as the current working directory. git merge bugfix // Merge bugfix into master.
git log -1 // -1, number one, not char "l".
git merge --abort // To stop a merging, but might not be that helpful. git merge-base BRANCH1 BRANCH2
Git Clone
The copy created with “git clone” is linked to the original and can send and receive changes back to the original. When you clone a repository, the only branch that appears in the clone is the active branch (the one HEAD points to) from the original repository.
1
git clone --bare // Creates a Git repository that consists of just this bare directory
You can’t perform any Git operations within the math.git directory, because there’s no working directory. But you can clone this math.git directory and push commits to it. A bare directory is that it has no reference to the original repository. The only way to update it is to push to it, and the only way to retrieve its contents is to clone, or pull, from it. I guess the project directory might be just a “bare directory”.
1 2 3
git clone --bare math math.git
git clone --no-single-branch -depth 1 // Enables you to produce a repository that consists of only one commit for all the branches in your repository.
1
git ls-tree HEAD // list the fils in HEAD in a tree-like style.
Git Remote
1 2 3 4 5 6 7 8
git remote // The simplest form of the git remote command shows only the name of the remote. git remote -v show // It shows the remote URL. git remote rename origin beginning // It renames the remote from origin to beginning. git remote add bob ../math.bob //This creates a new remote called bob, using git remote and "git remote -v show" to view it. git ls-remote //This command returns a list of the SHA1 IDs of each branch and tag (each reference) on the remote repository.
git remote remove git remote set-url
Git Push and Pull
1 2 3 4 5
git push origin master // Push the branch "master" to remote "origin". git remote –v show origin // Show the remote "origin" information. git push --force // blindly change the remote repo git push --set-upstream origin new_branch // push the newly created "new_branch" to the remote "origin", since upstream branch in the remote, we use the "--set-upstream" switch. git push origin :new_branch // To delete a branch in remote "origin", notice the colon before the "new_branch".
1
git push origin src:dest
When you omit the src from the full form, you are telling Git to delete the branch. git push origin master is equivalent to git push origin master:master.
1 2 3
git push origin TAGNAME // Push the tag named TAGNAME to the remote named origin. git push --tags // Push all tags to the default remote git push origin :TAGNAME // Delete the tag named TAGNAME on the remote named origin.
git pull consists of two commends: git fetch and git merge.
1 2 3 4 5
git pull --ff-only // This prevents Git from doing any automated work, unless it’s a fast-forward merge. git log --merges // display the merge operation inlog. git log --oneline readme.txt // limit the Git output the commits that affected "readme.txt" git log --grep=change // Display all commits that contain word "change"in the message. git log --since 10/10/2014 --until 10/24/2014 // Display the commits within a range.
1 2 3
git shortlog // An easy way to get the list of authors from a repository. git shortlog -e // To get the author list with email addresses. git log --author=watson // Restrict the log to one author.
1
git log --stat HEAD^..HEAD
It displays the number of files that have changed between the most current commit (HEAD) and its immediate predecessor (HEAD^), --stat switch shows a list of files that have changed. Double-period specifies a revision range. Ranges specify a set of commits. In this case, the two commits are next to each other (HEAD^ is immediately before HEAD).
1 2
git log --patch HEAD^..HEAD // "--patch" shows the content that have changed. git branch --column // To display the branches in columns.
1
git grep change // Looking for files that contain word "change".
Git rebase
1
git rebase master new_feature // Changing the starting point of the local branch new_feature with a new commit of master(Remote one).
1
git rebase master // The commend indicates to rebase the current branch, which is "new_feature"in the current scenario. The "rebase" commend would also assign new SHA1 ID to the commits in"new_feature".
1
git reflog
This command accesses an internal list of every change to the HEAD, it lets you reference old SHA1 IDs that may no long exist. This is important, since rebase changes the SH1 ID of the branch that you are rebasing.
1
git reset --hard HEAD@{4}
The --hard switch resets both the staging area and the working directory. After typing this command, HEAD is moved back to the previous position.
1
git rebase --interactive master
Interactively rebase your current branch with the latest commit from master. This opens an editor, allowing you to pick and choose which commits will be included in the rebase.
1
git -c log.date=relative log -n 2
This output shows the date using the relative date format. The behavior of the git log command changed, because you applied a new configuration via the -c switch of the git command. The -c switch is a fast way to override a configuration quickly. Remember, git -c is not the git config command!
1
git config --local --unset log.date
It resets the log.date configuration permanently by unsetting its value.