Working with git¶
You may have seen cheat cheat lists of different git command line commands. This document tries to be like that, but with practical descriptions.
git help
¶
Print help about git command.
git help diff
¶
Print help about git diff
command. You can change the command name.
git status
¶
Prints status of your working copy.
git branch
¶
Prints a list of your current branches.
git log
¶
Prints a list of previous commits.
git diff
¶
Evaluate changes in your working copy not yet marked to be added in the next commit.
git diff --cached
¶
Evaluate changes marked to be in the next commit.
git diff FILE
¶
Evaluate changes for a specific file in your working copy, not yet marked to be in the next commit.
git diff --cached FILE
¶
Evaluate changes for a specific file in your working copy, which are marked to be included in the next commit.
git diff --cached --name-only
¶
Print list of file names which will be modified in the next commit.
git commit
¶
Save changes marked to be committed as a single commit.
Git will open a text editor and ask you to write a commit message text.
git checkout branch-name
¶
This command will change to the branch named branch-name
if it exists.
git checkout -b new-branch-name
¶
This command will create a new branch based on your current branch with name
new-branch-name
and change to it.
git add .
¶
Mark all files under the current working directory to be added to (or removed from) in the next git commit.
git add FILE
¶
Mark a specific file to be added to (or removed from) in the next git commit.
git add -p FILE
¶
Sometimes you may want to only include few changes in the file.
You can use -p
flag and go through your changes. This will make it possible
to mark only some changes to the next git commit.
git stash
¶
Stashes your current cached (e.g. marked to be added in the commit) changes in
a stash, which you can later retrieve with git stash pop
.
git stash pop
¶
Fetches your latest stashed changes and adds them to your working copy.
git stash list
¶
List your changes.
git rm FILE
¶
Removes specific file and marks it to be removed in the next git commit.
In case you have already removed the file, it's possible to execute this command and mark it to be removed in the next commit.
git commit -m "My commit description"
¶
Save changes stored in your staging area as a single commit.
Git will not open a text editor since you provided it on the command line.
git reset HEAD~1
¶
Revert previous commit and save changes again as marked to be commited. You should be careful!
It's good practice only revert your own commits which you have not published outside yet.
git submodule sync --recursive
¶
Can be used to update local changes from the .gitmodules
file, e.g. if you
change URLs for the repository from SSH to HTTPS.
Changing default text editor¶
You can change the text editor git will use by changing environment variable
EDITOR
.
Many systems have vi
as the default editor. You may find nano
to be simpler.
export EDITOR=nano