Submodules:
git submodule update --init --recursive
CHERRY-PICKING
git fetch remote-name
git cherry-pick -x 8989ee0 (-x to keep a ref of the existing commit from where we are cherry-picking)
git pull is like running git fetch then git merge git pull --rebase is like git fetch then git rebase
SQUASHING
git rebase -i HEAD~n git rebase -i shaofcommit from where you want to start
Get into interactive mode by typing "i" pick oldest commit let it be as it's with pick prefixed For all below replace pick with s
:wq enter to save and quit or :x to exit
Next write the comment that you want to have in the next screen
- git commit
- git fetch remote-name
- git rebase remote-name/branch
- Force push
you may reorder if needed
REVERT VS RESET
Reset deletes your commit history revert adds one to reach a prev state
git reset --hard 0ad5a7a6
Force to a previous Commit git reset --hard -----> Local git push -f origin branchname -----> Push to Remote git log ---------> to get sha
git revert 8f937c6 git push origin branch
In case last commit was merge commit 8f937c683929b08379097828c8a04350b9b8e183 Merge: 8989ee0 7c6b236
git revert 8f937c6 -m 1 will get you the tree as it was in 8989ee0, and git revert -m 2 will reinstate the tree as it was in 7c6b236.
Let's say the bad commit (dddf) is not the top commit, but a slightly older one, e.g. the second last one. We want to remove it, but keep all commits that followed it. In other words, we want to rewrite the history and force the result back to current state.
git rebase -i dddf^ This will open an editor and show a list of all commits since the commit we want to get rid of and then force push
Fix a typo in one of the commits
This works almost exactly the same way as prev case, but instead of removing the line with the bad commit, simply replace its pick with edit and save/exit. Rebase will then stop at that commit, put the changes into the index and then let you change it as you like
A list of my commonly used Git commands
If you are interested in my Git aliases, have a look at my .bash_profile
, found here: https://github.com/joshnh/bash_profile/blob/master/.bash_profile
--
Command | Description |
---|---|
git init |
Initialize a local Git repository |
git clone ssh://git@github.com/[username]/[repository-name].git |
Create a local copy of a remote repository |
Command | Description |
---|---|
git status |
Check status |
git add [file-name.txt] |
Add a file to the staging area |
git add -A |
Add all new and changed files to the staging area |
git commit -m "[commit message]" |
Commit changes |
git rm -r [file-name.txt] |
Remove a file (or folder) |
Command | Description |
---|---|
git branch |
List branches (the asterisk denotes the current branch) |
git branch -a |
List all branches (local and remote) |
git branch [branch name] |
Create a new branch |
git branch -d [branch name] |
Delete a branch |
git push origin --delete [branch name] |
Delete a remote branch |
git checkout -b [branch name] |
Create a new branch and switch to it |
git checkout -b [branch name] origin/[branch name] |
Clone a remote branch and switch to it |
git branch -m [old branch name] [new branch name] |
Rename a local branch |
git checkout [branch name] |
Switch to a branch |
git checkout - |
Switch to the branch last checked out |
git checkout -- [file-name.txt] |
Discard changes to a file |
git merge [branch name] |
Merge a branch into the active branch |
git merge [source branch] [target branch] |
Merge a branch into a target branch |
git stash |
Stash changes in a dirty working directory |
git stash clear |
Remove all stashed entries |
git update-index --assume-unchanged path/to/file.txt |
Stops watching changes locally |
git update-index --no-assume-unchanged path/to/file.txt |
Starts watching changes localls |
Command | Description |
---|---|
git push origin [branch name] |
Push a branch to your remote repository |
git remote add [any_alias_for_remote_fork_repo] [fork_repo_url] git push origin [branch name] |
Add a new remote fork to pull changes from a fork/diff remote |
git pull [the_alias_for_remote_fork_repo] [branch name] |
Pull changes from a diff remote repository to the current branch |
git push -u origin [branch name] |
Push changes to remote repository (and remember the branch) |
git push |
Push changes to remote repository (remembered branch) |
git push origin --delete [branch name] |
Delete a remote branch |
git pull |
Update local repository to the newest commit |
git pull origin [branch name] |
Pull changes from remote repository |
git remote add origin ssh://git@github.com/[username]/[repository-name].git |
Add a remote repository |
git remote set-url origin ssh://git@github.com/[username]/[repository-name].git |
Set a repository's origin branch to SSH |
Reset to Upstream What if things are out of whack and you just want to reset your branch to the upstream version, losing anything that may be committed to your fork that you don't intend to pull request upstream? Follow these steps, originally described here:
git checkout master
git pull upstream master
git reset --hard upstream/master
git push origin master --force
Command | Description |
---|---|
git log |
View changes |
git log --summary |
View changes (detailed) |
git log --oneline |
View changes (briefly) |
git diff [source branch] [target branch] |
Preview changes before merging |
git reflog |
View recorded references to all changes to the git |