Git Commands Cheatsheet
git
github
version control
terminal
Basic Git survival guide.
- Initialise a directory as a repository which can track changes to files and subdirectories
git init - Check status in a directory
git status - Add a file to branch
- Single file
git add [filepath] - All files
git add .
- Single file

Figure 4: The main Git status sequence for a changing file.
- Commit changes with a short message of changes
git commit -m ['insert message'] - Show a log of commits
git log
- Check the differences between the master branch and the current branch
git diff - Push a file on the local repository to the remote repository
git push origin [branch name] - Create a configuration file called .gitignore as a text file which contains name of files and directories that will not be pushed to git
.gitignore- create file
touch .gitignore
- View current branches from master
git branch - Change from master branch: never work in the Master branch so create a separate branch:
git checkout -b ['name of new branch']git checkout [``'``name of branch``'``]- moves to named branch
- Merge changes from a branch to a master branch
git merge [``'``name of branch to merge to current branch``'``] - Clone repository to current directory
git clone [``'``github url from where to clone``'``] - Ensure master branch is up to date
git pull origin master - To copy a file from computer to a branch:
- Single file
cp ../foldername[i]/filename ./foldername[i]/filename
- Multiple files
cp ../foldername[i]/* ./foldername[j]/filenamecopies everything from foldername[i] to foldername[j]
- Single file
- Remove a file from the index git rm –cached
- Remove a directory from index git rm -r –cached
Putting existing work on GitHub
- Create a new repository on GitHub
- In the terminal, go to the directory that we want to push to Github - this is called the local directory
- Initialise the local directory as Git local repository
git init - Add the files in the Git local repository to a staging environment
git add . - Commit the files in the local repository
git commit -m"``First commit``" - Back in the Github Quick Setup page, copy the remote repository URL - this is where the files in the local repository will be pushed to.
- Back in the terminal, add the URL for the remote repository where the local repository will be pushed
git remote add origin {remote repository URL}git remote -v #Verifies the new remote URL - Push changes in the local repository to GitHub
git push -u origin master
Notes:
- If we wish to retrieve files from github
git pull origin [branchname] - To check where git is pulling and pushing files from and to
git remote
Setting up a gitignore file
- A .gitignore file specifies the files that you want github to ignore and not show in the repository on the website. We can choose to hide the .gitignore file itself by self reference in the .gitignore.
- To create a .gitignore file, go to the directory that you intend to push to github and create a text file labelled ‘.gitignore’. In this file, list all the files and directories you don’t want to push to github - let a new file/ directory be placed on a new line.
- An example of a .gitignore file is

All the files in this text file will not be pushed to github but still appear in your directory locally. - If we have files already in github that we would later like to untrack, we will have to include them into the .gitignore, remove them from the git index and recommit. To do this git rm –cached {filename}
Using Git Stash
- Usually when a repository is updated at its origin and we have local changes on the same branch, we will first push our new changes and make a Pull Request. Then the owner of the repo will merge the Pull Request. Once this is done, we will pull the updated repository.
- At other times, we want to pull the updated repository without submitting our local changes. In cases like this we can stash the changes and pull the repository from origin.
- Create a stash
git stashgit stash -a(include untracked and ignored files) - List stashes
git stash list - Save stash and add name to stash
git stash save {name of stash} - Retrieve stashed changes
git stash pop stash@{1} - Clean up the stash
git stash clear(removes all stashes)git stash drop {stash_id} - Check Stash diff
git stash show {stash_id}
References
- https://opensource.com/article/21/4/git-stash