Git is a commonly used version control tool. It is also a repository for source code and other project files. This page provides a basic overview of using Git from the command line as well as other useful info.
Configuration
The Git config tool can be opened by typing git config
at the command line. To view the config use git config --list
. The following are some useful commands.
git config --global user.name "My Name"
git config --global user.email myname@mydomain.com
To set up the default text editor (for example Notepad++ using Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -nosession"
Create New Repository
git init
To initialise a repository and create the .git directory.
git add *
To track files. A .gitignore file should also be used so that only desired files are tracked
git commit -m "first commit"
To commit changes on tracked files
git remote add origin https://github.com/myname/myrepo.git
Add remote
To remove remote: git remote rm origin
git push -u origin master
Push code to remote
Update Remote to Match Local
git commit -am "Updated Files"
The -a flag tells git to update all tracked files, the m flag tells git to add a commit message
git push origin master
-f
flag can be added to force update, only use if you wish to overwrite remote files!
If you wish to perform multiple commits and push them together, the rebase command can be used:
git rebase --interactive HEAD~2
would combine the previous 2 commits
Update Local to Match Remote
A git pull command will fetch data from the remote location (eg.Github) and merge it with the local files. It’s a good idea to do a pull request before working on some files, to make sure your base files match remote. This is especially important if working from multiple PCs.
git pull <remote>
Restore Local files
The git checkout
command can be used to restore local files to match the Master branch. This is useful if a local file is unintentionally deleted or modified and you do not wish to commit these changes. For this command to work the changes must not be staged/committed. You can use git status
to check if the changes are staged.
git checkout -- <file(s)>
Clone a Repository
To clone an existing repository from Github use the clone
command:
git clone https://github.com/myname/myrepo.git