Git common commands
- Git
Below a summary of the commands I use most frequently. For more explanation on Git concepts look at my previous articles: Git basic concepts and Git environment
Clone a repository
To clone a remote repository to a folder with the same name as the repository ("sample" in this case):
git clone git@github.com:tm/sample.git
To get the local environment setup in a different folder, add the folder as a parameter
git clone git@github.com:tm/sample.git <other-folder>
Commit changes
To move changes to the staging area use the add
command. With the .
or -A
all file changes are taken into account. With -u
only modified and deleted files are considered (not new files).
git add .
git add -A
git add -u
Add staged files to the local repository. When the commit
is given without a commit-message the default Git-editor will be opened to enter a message.
git commit
git commit -m "Fix typo in file"
The normal flow is staging files to mark them for the next commit. With the -a
flag you can skip staging and commit all changed files directly.
git commit -a -m "Fix another typo in file"
To correct the previous commit or commit-message use the --amend
flag. It will completely replace the previous commit.
git commit --amend
To transfer the commits to the remote repository use the push
command:
git push
git push <remote> <branch>
Get updates from a repository
When you fetch
, only the local repository gets updated. The --prune
flag clears information from you local repository that is no longer present in the remote repositoy (like deleted branches).
git fetch
git fetch --prune
pull
also updates the working directory by merging the changes.
git pull
--rebase
flag. This will keep the commit history cleaner.
Several other common commands
Status
Get information about the current state of your local environment. Optionally add the --short flag
to get a compact summary.
git status
git status --short
Compare
Get information about the changes in files.
Compare the working directory to the index.
git diff
Compare the index to the local repository. The staged
and cached
flags have the same result.
git diff --staged
git diff --cached
Remove a file
Remove a file from Git. The file will be deleted from the working directory and index.
git rm <file>
When you want to remove a file that has been modified or staged you can use the -f
flag (force delete), but be sure to know that you want your changes to be deleted.
git rm -f <file>
Rename a file
Rename a file and add it to the index. Under the hood, this command will remove the <from>
file Git (git rm
) and add the <to>
file to index.
git mv <from> <to>
Remotes
Show the remotes that are available.
git remote
Log
Get information about the commit history where the commits are ordered from newest to oldest. Some options are mentioned below but there are a lot more so check the documentation on this.
git log
git log --patch
git log -<number of commits>
git log --graph
git log --pretty=oneline
git log --no-merges