Presenting my collection of useful git commands. Just so that I can remember them all when I need to.
When text=auto normalization is enabled in an existing repository, any text files containing CRLFs should be normalized. If they are not they will be normalized the next time someone tries to change them, causing unfortunate misattribution. Read more about this here.
So from a clean working directory:
echo "* text=auto" >>.gitattributes
rm .git/index # Remove the index to force git to
git reset # re-scan the working directory
git status # Show files that will be normalized
git add -u
git add .gitattributes
git commit -m "Introduce end-of-line normalization"
Revert to a previous commit
This command is a nice one, if the present build is broken (for some reason) – and you need to quickly revert to a build you know works (while someone else figures out the reason why the present build is broken).
git reset --hard 62fbb3
Revert a file
You’ve written something in a file and now you wish to regret and revert that change entirely.
git checkout -- filename
You’ve written code in commit 62fbb3 in the feature branch. It may contain a bug fix or code that other people need to have access to. Whatever the reason, you want to commit 62fbb3 in the master branch – but not all the other code you’ve written in the feature branch. So you cherry-pick the commit.
git checkout master
git cherry-pick 62fbb3
62fbb3 is now added and commited as a new commit in the master branch.
git log branch1..branch2 --no-merges
Show the commits in branch2 but not in branch1. Very useful if you’re comparing branches. The no-merges flag is only necessary if you want to exclude merges.
Recover a file that was accidentally deleted:
git checkout HEAD^ filename
This works if you know the exact name of the file.
git config --global user.name myusername
git config --global user.email myemail
git config --global push.default simple
git config --global core.autocrlf true
I find that push.default simple is better than push.default matching. Matching could lead to certain doom. Also this is an excellent link to understand autocrlf.
To list all of your settings:
git config --list
You’ll find yourself needing to stash code in a drawer sometimes:
You can list the contents of the drawer:
git stash list
To return to the stashed code, apply it:
git stash apply --index stash@2
The index parameter is to also restage any staged files.
The stash stays on until you remove it:
git stash drop stash@2
Create a local feature branch of master (and switch to it at the same time):
git checkout -b feature master
Tracking is the default behavior when the start point is a remote-tracking branch. So there’s no need to include the –track option, if that is what you want. BTW the above command is shorthand for:
git branch feature
git checkout feature
Push the feature branch to the remote repository:
git push -u origin feature
Delete a local feature branch:
git branch -d feature
Push the deleted feature branch to the remote repository:
git push origin :feature
Create a local tag:
git tag 3.00.01
Push the tag to the remote repository:
git push --tags
Delete a local tag:
git tag -d 3.00.01
Push the deleted tag to the remote repository:
git push --delete origin 3.00.01
Pulling and fetching
Pull to get all the changes from the remote branch and merge them (if possible) into your local branch at the same time.
Fetch to get the changes from the remote branch to your local repository without doing the merge with your local branch as well.
Then do a diff to see what the fetched changes are:
git diff origin/master
Then do a merge to actually merge the changes with your local branch.
git merge origin/master
More documentation at git-scm.com.