In this post, I share some useful GIT commands for everyday use. After years of using GIT, I’ve found useful commands to make my developer’s life easier. This is the list of the most frequently used in my case.
I’ve created the Github demo repo here, containing all of these commands. I will try to keep this post up to date when other useful commands came out. If you have another useful command I missed, your comments are welcome!
Clone a repository
How to clone a repo creating the folder with a different name (eg: git_commands
instead of the default name git-commands
)
git clone https://github.com/fraigo/git-commands.git gitcommands cd gitcommands
To clone a single branch from a repository (useful only when the repository is very large and contains binary files that slow down the performance of the repository) [Since Git 1.7.10]
git clone --single-branch --branch my-branch https://github.com/fraigo/git-commands.git
Cleaning a repository
Discard all changes to modified files in your repo. Only staged files (exclude untracked files)
git reset --hard HEAD
Discard all untracked files (You can check first which files would be removed with git clean -n
)
git clean -f
Restore modified files (not yet committed)
git checkout filename
Unstage all files (remove them after running git add
) or, optionally, unstage some specific file(s) adding -- path/to/file
git reset [-- path/to/file]
Revert the last commit (locally, before pushing to remote). This will remove the last commit. All committed files will be returned to stage (pre-commit). You can use it, again and again, to go back one step more.
git reset --soft HEAD~1
In case you want to revert the very first commit in your repository, use this command:
git update-ref -d HEAD
Pulling changes
Pull changes into a forked repo from a branch in another source repo:
git pull https://github.com/fraigo/other-repo.git source-branch
Another way more consistent is to save the repo url as a remote source with alias (upstream
in this case).
git remote add upstream https://github.com/fraigo/other-repo.git git fetch upstream
In this way, you can use the source repo for any other operation referencing branches as, for example, upstream/my-branch
. For example, pulling remote changes and then replay your commits on top of that branch
git rebase upstream/my-branch
Merging changes
If you get a merge conflict and you can go back to pre-merge state (abort), removing merged files and conflicting versions as well:
git merge --abort
If your want to resolve conflicts using the remote version (from the branch you are merging) of that files, abort the merge and then:
git merge -Xtheirs [branch-name]
In the opposite case, you can merge using the local version of the conflicted files using:
git merge -Xours [branch-name]
Pushing
How to push local to the remote (master) repository for the first time (from a locally-created repo to an empty repo)
git remote add origin https://github.com/fraigo/git-commands.git git push -u origin master
The -u parameter is to setup this origin as the upstream. So any next call to git push
or git pull
does not need to specify origin/branch when working on this branch.
Diff
In some cases, we need to check if a branch is up-to-date or different from another one (including the master branch).
Show only a list of different files
git diff --name-only
If you need more information about the difference you can use –stat to see a more graphical diff statistic, or –numstat to see only a number of changes (additions/deletions).
git diff --stat
git diff --numstat
Refactoring
Reset your repo to a specific commit (all changes after this commit will be removed). You can see the commit hashes using git log
git reset --hard e8fe18df3a8ff8220b9158f53a37dc163f45bc67
Forcing a remote repo to match the local repo (also for branches)
git push -force origin master
Working with branches
List current branches
git branch
Create a new branch in your local repo and switch to that branch
git checkout -b new-branch
Also, the first push of a new branch to a remote should be (-u
is equivalent to the option --set-upstream
)
git push -u origin new-branch
In this way, you can push and set up to track the remote branch origin/new-branch
Checkout/Switch to an existing branch
git checkout test-branch
Deleting Branches
Delete a branch locally and remotely (the -r
option is to delete it remotely)
git branch -d -r my-branch
Also if you already deleted the branch locally, without the -r
option:
git push origin --delete my-branch
Also, if you still see the branch in the remote repository, supposedly deleted, you need to prune the origin:
git remote prune origin
After deleting the branch you can sync your local branches using
git fetch -p
Related articles
Configure Git for the first time