Configuring
System-level configuration, for the entire PC
c:\Program Files (x86)\Git\etc\gitconfig
or ~\.gitconfig
on MAC. Accessed by:
git config --system
User-level configuration, stored in c:\users<name>.gitconfig
git config --global
Repository level configuration, stored .git/config
git config
Show all global options
git config --global --list
Setting properties on global
git config --global user.name
git config --global user.email
Configuring core editor
git config --global core.editor "'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Configuring beyond compare
git config --global diff.tool bc3
git config --global difftool.bc3.path "c:/program files (x86)/beyond compare 3/bcomp.exe"
git config --global difftool.prompt false
git config --global merge.tool bc3
git config --global mergetool.bc3.path "c:/program files (x86)/beyond compare 3/bcomp.exe"
git config --global mergetool.prompt false
git config --global mergetool.keepbackup false
git config --global mergetool.keeptemporaries false
Configuring auto correct for the git commands
git config --global help.autocorrect 1
Use colors to show a lot of git information
git config --global color.ui auto
Auto carriage return line feed. False is for windows only will submit it as is. True will convert. Input is for mac, it will convert crlf into lf.
git config --global core.autocrlf false
To remove a setting
git config --unset user.name
You can also edit the config files yourself.
Working Locally
Creating a local repository
git init
git status
git add file.txt
git status
git commit
Look at the history and see the commit SHA: git log
Add all modified/updated files
get add -u
Commit with message
git commit -m "Message"
To view the diffs between SHA1 hashes
git diff dd2929..333322
Easier, latest commit is know as HEAD
git diff HEAD~1..HEAD
git diff HEAD~1..
Add all files including untracked files
git add -A
Stage as two different commits, add specific files and then commit.
git add file1.txt
git commit
Deleting Files
Delete the file first. Then stage the deletion with
git add -u
Rename the file first then:
git add -A
To clear out unstaged changed to the README.txt file
git checkout README.txt
To do a global clear out, and remove all unstaged changes git reset –hard
To take out the last commit out of the repository and move back to the staging area, that way you can change that commit around
git reset --soft HEAD~1
make changes
git commit -m "reorganized commit"
To delete last commit and discard all the changes
git reset --hard HEAD~1
Remove unstaged files
git clean -n //what would it do
git clean -f //performs the operation
.gitignore
Create a file that will list all of the files and directories that need to be omitted.
Working Remote
clone repository
git clone https://....
see list of commits
git log
commits per line
git log --oneline
see number of commits
git log --oneline | wc -1
see the graph git log –graph
Authors and commit messages
git shortlog
Statistics about authors
git shortlog -sne
Look at the specific commit
git show HEAD
git show HEAD~10
git show 939393
Look at the remotes, origin is the git default where the repository came from
git remote
git remote -v //verbose shows remote urls
To see the repository config cat .get/config
Display all the branches in the repository
git branch
Display all the remote branches
git branch -r
Look at the tags, known stable versions
git tags
To add a remote repository, is commonly down to evaluate patches and pull requests into your project
git remote add origin https://github.com/....
Once remote repository is added you can
git fetch //all
git fetch origin
to see changes on remote repository
get log origin/master
Merge remote branch into the your current branch
git merge origin/master
Fast forward means, replay commits that happen if there were no other changes and move the HEAD
Shortcut for git fetch; git merge origin/master
is
git pull
To set your branch to mirror some other remote branch, or remote tracking branch, or upstream tracking.
git branch --set-upstream master origin/master
If you can also specify without setting the remote like this. origin is the remote name and master is the remote branch.
git pull origin master
Git commit and add modified files together
git commit -am "Some message"
Push your changes to the remote git push
To remove the origin on the current branch
git remote rm origin
to tag a current version, or stable point
git tag v1.0.0
to create tag with the annotation
git tag -a v1.0
To create a tag with the signature, signed with the password
git tag -s v1.0
To verify a tag
git tag -v v1.0
To push tags to remote (by default git would not push tags)
git push --tags
Branching Merging and Rebasing with Git
list of the commits on the current branch with a graph
git log --graph --oneline
To visualize all branches, decorate applies the branch and tag information
git lot --graph --oneline --all --decorate
Adding aliases in the git config
git config --global alias.lga "log --graph --oneline --all --decorate"
git config --global alias.lg "log --graph --all --pretty=format:'%C(bold)%h%Creset -%C(auto)%d%Creset %s %C(green dim)(%cr)%Creset %C(ul)<%an>'"
git lga
git lg
cat ~/.gitconfig
To Create a new branch git branch feature1
Checkout new branch
git checkout feature1
Big difference between branches and tags is that tags always stay on the same commit where branches move along.
git checkout master
Create a branch from a specific commit
git branch fix1 9929292
git checkout fix1
To rename a branch is to move it
git branch -m fix1 bug1234
To delete a branch
git branch -d bug1234
To force delete a branch
git branch -D bug1234
To create a branch and checkout (shortcut) git checkout -b feature2
To get back a deleted branch. From a dangling commit, a commit that doesn’t have a home. These are kept for 30 days.
git reflog // log of all references
git branch bug1234 5a78c8b //with the first 8 of the SHA
To see head
git show HEAD
Save half completed work is to stash it. Temporary holding area for the changes that are not committed to the branch.
git stash
git status
git stash list //holding area
git stash apply //to pull the changes back
git stash pop //applies it and removes it from the list
Merging
Merge fiture1 into the current branch
git merge feature1
git branch -d feature1 //deletes unused branch
Fast forward merge, is just moving the branch label into the new location.
To resolve conflicts using the merge tool
git mergetool
Compare the repository to the staging area
git diff --cached
Rebasing
You can play back your commits on a certain branch rather creating a branch if there are no conflicts. Rebase current branch on top of master
git rebase master
You can also use mergetool to solve merge conflicts and then continue
git rebase --continue
Cherry Pick
If you need to get one commit and apply it on top of a branch.
git cherrypick sks939ss
#Working with remote
git fetch origin master //get latests
git push origin master
to push a branch to remote
git push origin v1.0_fixes
list remote branches
git branch -r
To delete remote branch, push to the remote without specifying the local name
git push origin :v1.0_fixes_remote_branch_name