Committing and Undoing
Adding file for committing
$ git add <filename>
or
$ git add .
Executing Commit
$ git commit -m "This is my comment"
or adding + committing in one
$ git commit -am "My comment"
Undoing all changes
Reverts all changes to the last commit
$ git reset --hard
Jumping back to a given SHA
## reset the index to the desired tree
git reset 56e05fced
## move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
## Update working copy to reflect the new commit
git reset --hard
Removing a tracked file
To remove a tracked file from git but not from the local file system use
git rm --cached <filename>
Merging
Merge single file(s)
$ git checkout <local-branch-name> <file-path>
Undo a merge
git reset --hard commit_sha
or simply
git reset --hard origin/HEAD
to reset to the last remote commit. Attention, undo obviously only local merges.
Update from original repo
$ git remote add upstream <org_git_url>
Then you can fetch it normally through
$ git pull upstream <branch>
Branches
List all branches
$ git branch -v
Create Branch
Checks out a branch and automatically switches to it.
$ git checkout -b <new-branch-name>
Download a remote branch
$ git checkout -b local-branch-name origin/remote-branch-name
Publish a local branch
$ git push -u origin <local-branch-name>
Delete local branch
$ git branch -D bugfix
Delete remote branch
$ git push origin :<remote-branch-name>
Delete tags
Local tags
git tag -l | xargs git tag -d
Remote tags
$ git ls-remote --tags origin | awk '/^(.*)(\s+)(.*[0-9])$/ {print ":" $2}' | xargs git push origin
Sync with a remote fork
First add a "remote" to the original fork
git remote add upstream https://github.com/otheruser/repo.git
...then you can update just normally using
git pull upstream master
Submodules
Initialize
To initialize and update existing submodules simply use
git submodule init
git submodule update
Update
To update all submodules in the sense of performing a git pull
on all of them you can use
git submodule foreach git pull
which will iterate through each of the submodule.
Git Configuration
Pretty logs
Add an alias to your ~/.gitconfig
like
[alias]
lg = log --graph --full-history --all --color --pretty=tformat:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s%x20%x1b[33m(%an)%x1b[0m"
Alternatively (as described here )
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Autocompletion
Download this file to ~/
and rename it to .git-completion.bash
. Then open .bash_profile
and add
source ~/.git-completion.bash
Autocorrect
git config --global help.autocorrect 1
Automatically create version tag
grep -Eo "AssemblyVersion\(\"(.*)\"\)" AssemblyVersionInfo.cs | cut -d\" -f2
Reference Table
Command | Work description |
---|---|
add | Add file contents to the index. When new or modified files are ready for the next commit, they must first be staged with the add command. Files can be staged one by one, by folder name, or by wildcard pattern. |
bisect | Find by binary search the change that introduced a bug |
branch | List, create, or delete branches |
checkout | Checkout a branch or paths to the working tree |
clone | Clone a repository into a new directory |
commit | Once all desired files are added and staged, a commit command transactionally saves the pending additions to the local repository. |
diff | Show changes between commits, commit and working tree, etc |
fetch | Download objects and refs from another repository |
grep | Print lines matching a pattern |
init | Create an empty Git repository or reinitialize an existing one |
log | Show commit logs |
merge | Join two or more development histories together |
mv | Move or rename a file, a directory, or a symlink |
pull | Fetch from and merge with another repository or a local branch |
push | Update remote refs along with associated objects |
rebase | Rebasing is the rewinding of existing commits on a branch with the intent of moving the branch start point forward, then replaying the rewound commits. This allows developers to test their branch changes safely in isolation on their private branch just as if they were made on top of the mainline code, including any recent mainline bug fixes. |
reset | Reset current HEAD to the specified state |
rm | Remove files from the working tree and from the index |
show | Show various types of objects |
status | Show the working tree status, and check the current status of a project’s local directories and files, such as modified, new, deleted, or untracked. |
tag | Git provides tagging to mark a specific commit in your timeline of changes, and serve as a useful identifier in history. A tag can be created for any commit in history by passing the commit hash. If no commit hash is provided, the most recent commit (i.e. HEAD) will be used by default. |