Useful Git commands
This file contains a list of common and hopefully useful basic git commands. It basically lists all the commands from the Radboud Git courses in gROW. The slides have their own dedicated webpage. You will find that the commands have been sorted below according to the presentation they are in, and subsequently in alphabetic order.
If you find any mistakes or omissions, please let me know.
Git basics
git add <file-name>
Adds the file(s) provided as an argument to the staging area. You can provide more than one file.
git add .adds all changes in the current folder and subfolders to the staging areagit add -Aadds all changes in the repo to the staging areagit add -popens git interactive interface, which uses the following commands:Command What does it do? yStage this hunk. nDo not stage this hunk. qQuit. Do not stage this hunk or any remaining hunks. aStage this hunk and all later hunks in the file. dDo not stage this hunk or any later hunks in the file. /Search for a hunk matching the given regex. eManually edit the current hunk. ?Print help.
git commit
Opens default terminal editor to create commit message. After closing the editor a commit is created on current branch.
git commit –m “a message briefly saying what you changed and why”the editor is not opened. Instead the supplied string is added as the commit message- Note that a message has a 72 character limit, whereas the description can be longer.
git commit -m "Message" -m "Description"adds a message, as well as a longer description.
- Note that a message has a 72 character limit, whereas the description can be longer.
git commit --amendinstead of creating a new commit, the previous commit is updated. The default text editor is opened so you may modify the original message.git commit --amend -m "my updated commit message"the same as above, but uses the supplied message to update the previous commit
git config
Allows you to set options for your repositories:
--local>--global>--systemapplies config to respectively all users on system, current user or only current project.--localis the default- Local: applies configuration to current repo only
- Global: applies configuration to all repos owned by the user
- System: applies configuration to every user on the system and all their repos. Requires admin rights.
git config --global user.name "Jane Doe"sets user name for all your projects to Jane Doegit config --global user.email jane@example.comset user email for all your projects to jane@example.comgit config --helpopens help page on Git config
git help
Takes any of the other commands as an argument and provides the relevant documentation
git help helporgit help --helpshows how to use this versatile command- You can also add
--helpto any command to get information on its usage, e.g.git add --help - Note to Git Bash for Windows users: in some cases documentation is opened in your browser rather than in the CLI.
git init
Initialises a Git repo in present folder and subfolders.
git init ../path/to/other/folderadds an optional path as an argument to initialise the repo in a different folder than current
git log
Opens a log showing an overview of the repository’s commit history, including each commit’s unique identifier, author, date and time of change, ancd the original commit message.
git reset
Undoes previous commit and resets the previous changes to untracked.
- Note: This does not work after first commit of new repo!
git restore --staged <file name>
Unstages the changes from the file that was provided.
git rm --cached <file name>
Unstages the changes from the file that was provided.
git status
Outputs the repo’s status, like files that were added, removed or changed, the current branch etc.
git version
Shows version of Git on your machine
Git branching and teamwork
git branch --delete target-branch
Deletes ‘target-branch’. Instead of --delete you can also use -d. Note that you cannot delete a branch if:
- You are currently in the target branch
- If the branch has not bee merged into the upstream (i.e. parent) branch.
- If you still wish to delete the branch, you can force it with
git branch --delete --force target-branchorgit branch -D target-branchfor short
- If you still wish to delete the branch, you can force it with
git branch --list
Shows a list of all your branches. Current branch is indicated by *.
- Shorter versions of this command are
git branch -landgit branch
git branch new-branch-name
Creates a new branch with the name ‘new-branch-name’.
git checkout target-branch
Switches to the branch named ‘target-branch’.
git checkout -b target-new-branchwill create a new branch called ‘target-new-branch’ and switch to it afterwards- See also
git switch, which is now the recommended command for switching branches.
git clone url/to/repository
Clones a repository from the provided URL, which often starts in either ssh:// or https://.
git merge branch-name
Merges branch-name into your current branch.
git pull
Pulls the latest changes from the remote repo and merges them into your local branch.
git push
Uploads your local commits to the remote repo branch of the same name.
git push -u origin branch-nameuploads local commits explicitly to remote repo branch of name ‘branch-name’- The option
-u(short for--set-upstream) links your local and upstream branch, making the connection a default
- The option
git switch target-branch
Switches to the branch named ‘target-branch’.
git switch -c target-new-branchwill create a new branch called ‘target-new-branch’ and switch to it afterwards- See also
git checkout, the old command for switching branches.