Configuration
Set up global configuration variables if you haven’t done so
$ git config --global user.name "<name>"
$ git config --global user.email "<email>"
Git should automatically do a rebase when you do a pull, which is what you want
$ git config branch.autosetuprebase always
To set up configuration for DiffMerge
, follow the guide
http://coding4streetcred.com/blog/post/configure-diffmerge-for-your-git-difftool.
Local Usage of Git
Staging
Check if there is any unstaged or untracked files
$ git status
Add files to the staging
$ git add <file.name> // add a specific file
$ git add . // add all files from the current folder
$ git add myfolder/ // add all files from the subfolder 'myfolder'
$ git add (-A) // add all files from entire working tree, even from upper directory
$ git add --no-all myfolder/ // add all but deleted files from myfolder: ``
$ git add -u or --update // add all but new or untracked files
Remove files from the staging
$ git reset HEAD <file.name> // remove a specific file
$ git reset // remove everything from the staging area
Reset
$ git checkout -- <filename> // discard the changes in a file
$ git reset --soft HEAD~1 // leave all your changed files "Changes to be committed", as git status would put it
$ git reset --soft <commit hash> // move back to staging dir and keep the modification
$ git reset <commit hash> // move back to working dir and keep the modification
$ git reset --hard <commit hash> // revert tracked files back before modification
$ git clean -df // get rid of any untracked dir and files
$ git reflog // lifesaver if you accidently delete something important
$ git revert <hash> // ?
Commit
Commit changes
$ git commit -m "<messages>" // commit with a message created
$ git commit --amend -m "<new messages>" // edit the commit message
$ git commit --amend // commit changes to the previous commit
Show commit history
$ git log
How to write a great Git commit message
- Separate subject from body with a blank line
- Limit the subject line to 50 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line
- Wrap the body at 72 characters
- Use the body to explain what and why vs. how
Edit a specific commit
$ git rebase -i @~3 # Show the last 3 commits in a text editor
Find the commit you want, change pick
to e
(edit), and save and close the file.
Git will rewind to that commit, allowing you to either:
use
$ git commit --amend
to make changes, oruse
$ git reset @~
to discard the last commit, but not the changes to the files (i.e. take you to the point you were at when you’d edited the files, but hadn’t committed yet).
Then run $ git rebase --continue
and Git will replay the subsequent changes on top of your modified commit. You may be asked to fix some merge conflicts.
Note: @
is shorthand for HEAD, and ~
is the commit before the specified commit.
Other approaches: https://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit-in-git
Squash several commits into one
Step 1: Invoke git to start an interactive rebase session:
$ git rebase -i HEAD~[N]
where N is the number of commits you want to join, starting from the most resent one. For example $ git rebase -i HEAD~3
.
OR if you have too many commits to count, try
$ git rebase -i [commit-hash]
where [commit-hash]
is the hash of the commit just before the first one you want to rewrite from. For example, $ git rebase -i 5392bc
to join the top 3 commits in below
fdascc Fix at 13:00
asfdsd Fix at 12:00
kgfdas Fix at 11:00
5392bc Fix at 10:00
Step 2: Picking and squashing
At this point your editor of choice will pop up, showing the list of commits you want to merge in a reverse order. For example,
pick kgfdas Fix at 11:00
pick asfdsd Fix at 12:00
pick fdascc Fix at 13:00
Our task here is to mark all the commits as squashable, except the first/older one: it will be used as a starting point. You mark a commit as squashable by changing the work pick
into squash
next to it (or s
for brevity, as stated in the comments). The result would be:
pick kgfdas Fix at 11:00
s asfdsd Fix at 12:00
s fdascc Fix at 13:00
Save the file and close the editor.
Step 3: Create the new commit
You have just told Git to combine all three commits into the the first commit in the list. It’s now time to give it a name: your editor pops up again with a default message, made of the names of all the commits you have squashed.
You can leave it as it is and the commit message will result in a list of all the intermediate commits, or wipe out the default message and use something more self-explanatory.
Branch
You can edit and commit under a branch which wont affect the master branch at all
$ git branch // check which branch you are currently in (with *)
$ git branch <branch_name> // create a branch
$ git checkout <branch_name> // switch to the selected branch
$ git cherry-pick <commit hash> # bring the commit to the current branch
Merge Branch
$ git checkout master
$ git merge <branch_name>
$ git branch --merged // check if it's merged successfully
Delete a branch
$ git branch -d <branch_name> // delete branch locally
$ git push origin --delete <branch_name> // delete branch remotely
Stash
You can save changes in a temporary place so you can work on other things and come back later. Note that stash won’t create anything to commit.
$ git stash save "<message>" // save changes in stash
$ git stash list // check existing stash
$ git stash apply <stash name> // fetch the changes in the stash
$ git stash pop // apply changes in the top stash (the newest) and drop it from the stash list
$ git stash drop <stash name> // drop the selected stash
$ git stash clear // remove all the changes made
Visual Tool
DiffMerge
$ git difftool
$ git mergetool
Gitk
$ gitk
Others
Check changes made in the code:
$ git diff <old hash> <new hash>
Create an ignore file:
$ touch .gitignore
The .gitignore file is a text file that list the files you want to ignore for tracking.
Remote Usage of Git
Clone a project:
$ git clone <url> <where to clone>
$ git clone <url> . // clone to current directory
View info about the remote repository:
$ git remote -v // list repository info
$ git branch -a // list all the branches, localy and remotely
Pull from the remote repository
$ git pull origin master
Push changes to the remote repository after making local commitments:
$ git push origin master // push to Git
$ git push origin HEAD:refs/for/master // push to Gerrit
Push branch to the remote repository
$ git push -u origin <branch_name> // -u is used so in future you can just simply use $git pull and $git push
References
Git Development workflow: https://wcdma-confluence.rnd.ki.sw.ericsson.se/display/TAE/WMR+GIT+Development+workflow
Git Basic Commands: https://wcdma-confluence.rnd.ki.sw.ericsson.se/display/TAE/Git+Basic+Commands
Git Gerrit: https://wcdma-confluence.rnd.ki.sw.ericsson.se/display/TAE/Git+Gerrit
More:
https://gerrit-review.googlesource.com/Documentation/intro-user.html#gerrit