A personal cheat sheet that covers basic Git commands and information for reference.
The following is a compilation of all the useful commands/resources/information I've found while learning Git. This document was created to help better my understanding of Git. So please support the official resources provided throughout the document.
- Codeacademy's Learn Git Course
- ProGit eBook
- Atlassian's Tutorial on Git
- Interesting blog post that explains Git/Github in plain english
- Interesting blog post on how to write a git commit message
- Similar Git Cheatsheet
- Another Git Cheatsheet
- Last Git Cheatsheet
The directory C:\Program Files\Git\mingw64\share\doc\git-doc holds interesting documentation for all of the popular commands.
Configuration | Back to ToC
$ git help
$ git config --global user.name "John Doe"
$ git config --global user.email "johndoe@example.com"
$ git config --global --unset user.name
$ git config --global --unset user.email
The top answer of this stackoverflow question states that in order to properly sign out and be asked again for username/password when commiting to a project, this needs to be done:
Go to: Control Panel -> User Accounts -> Manage your credentials -> Windows Credentials -> Generic Credentials
There, you fill find some credentials related to Github. Click on them and then click "Remove".
I found that this method simply deletes a personal access token from your github account. In doing so, yes, it is impossible to push a commit without signing into an account first.
However I noticed that this method fails in a certain regard. Suppose I want to push a commit from an account that is different from the account's default identity. Using the above method will not work because commits in Git is associated with the user.email key. In doing so, I would've made a commit from an account that is associated with the value of the user.email key, and not from the account that I typed into this alert window:
Better yet, if I unset the user.name and user.email keys, I recieve this message when I attempt to make a commit:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'Razat@DESKTOP-UK2NEC1.(none)')
How to confirm that user.name & user.email keys either have values or are empty from configuration settings:
$ git config --list
OR$ git config -l
OR$ git config user.name
AND$ git config user.email
Basic Git Commands | Back to ToC
A Git project can be thought of as having three parts:
- A Working Directory: where you'll be doing all the work: creating, editing, deleting and organizing files
- A Staging Area: where you'll list changes you make to the working directory
- A Repository: where Git permanently stores those changes as different versions of the project
The Git workflow consists of:
- editing files in the working directory
- adding files to the staging area
- saving/commiting changes to a Git repository.
Git essentially has 4 main states for the files in your local repo: Source
-
untracked: Untracked files are files that Git has no record of. In most cases, these files are typically new, or have been removed from the git repo. For example, if I have a new file, and I run
$ git add <file>
, it becomes: -
staged: The file is now in the staging area. Git now knows that the file exists, and the files are now being tracked. It is now part of the next commit batch (called the index). If you do a commit, it becomes:
-
unmodified/unchanged: The file has not changed since its last commit. If you modify it, it becomes:
-
modified/unstaged: The file has been modified but is not part of the next commit yet. If you run the
$ git add
command, the file will now be staged. In which case, the file will be staged AND modified from the previous commit.
Please note that the last three states are all considered to be tracked.
Also: You can untrack an uncommited file with git rm --cached filename
In Git, the commit you are currently on is known as the HEAD commit. In many cases, the last, most recently made commit is the HEAD commit.
$ git init
- NOTE: This command creates a hidden folder named `.git` in your working directory.
$ rm -rf .git
- NOTE: As an alternative, you can just manually delete the folder `.git` from the file directory.
$ git add .
$ git status
To see a short status of of files, use
$ git status -s
$ git diff
$ git commit -m "message"
$ git commit -a -m "message"
Run this command initially if you're pushing to a new Github repo:
$ git remote add origin [Github Repo URL].git
$ git push -u origin master
The
-u
option is only needed if the remote branch doesn't yet exist
Run the command
$ git push -f origin master
to force a push to a repo that contains work that you don't have locally.
Using this command will fix this error:
To git@github.com:roseperrone/project.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:roseperrone/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git log
- NOTE:
In the output, notice:
* A 40-character code, called a SHA, that uniquely identifies the commit.
> From now on, whenever I say 'SHA', I will refer to the first 7 digits of this number.
* The commit author
* The date and time of the commit
* The commit message
To show a log of each previous commit printed on individual lines, use:
$ git log --oneline
OR if you want to see the full SHA number, use:$ git log --pretty=oneline
To show the log of the last two commits:
$ git log -2
To only display commits of a particular file:
$ git log [file]
When you run this command, you will need to press the "Enter" key to see one previous commit, or the "Space" key to see a bunch of previous commits.
After running this command, if you're stuck on the page, you may need to press 'q' on your keyboard to restore the terminal.
Show list of modifiled files, how many files were changed, and how many lines in those files were added and removed in last two commits:
$ git log -2 --stat
$ git log -2 -U0
$ git show SHA
To see some abbreviated stats for each commit:
$ git show SHA --stat
To see the HEAD commit, enter: git show HEAD
$ git show SHA:[filename.fileextension]
Source
To see a list of modified files, how many files were changed, and how many lines in those files were added and removed.
$ git diff SHA1 SHA2 --stat
$ git diff -U0 SHA1 SHA2
Source
This also works:
$ git diff --unified=0 SHA1 SHA2
$ git diff --name-only SHA1 SHA2
Source
This works too:
$ git show --name-status SHA1
$ git diff-tree --no-commit-id --name-only -r SHA
$ git ls-files
$ git rm [file]
Remember: use this command to remove files from both the staging area and the working directory. It also stages the deletion for you.
Use
git rm --cached [file]
to remove the file from version control but the command preserves the file locally.
In order to remove a file with this command, it must first be under git version control. Files that have not been added yet (Untracked files) or are ignored (using .gitignore or .git/info/exclude files) will not be added.
$ git mv [file_from] [file_to]
$ git shortlog -sn
git config --global core.editor [editor]
To see what default text editor is being used, use:
$ git config core.editor
How to list remote branches that have been tracked locally: Source
$ git branch -r
Undoing Changes | Back to ToC
Viewing an old commit | Source
Let’s say your project history looks something like the following:
$ git log --oneline
b7119f2 Continue doing crazy things
872fa7e Try something crazy
a1e8fb5 Make some important changes to hello.txt
435b61d Create hello.txt
9773e52 Initial import
You can use git checkout to view the "Make some import changes to hello.txt" commit as follows:
$ git checkout a1e8fb5
This makes your working directory match the exact state of the a1e8fb5 commit.
You can look at files, compile the project, run tests, and even edit files without worrying about losing the current state of the project.
Nothing you do in here will be saved in your repository.
To continue developing, you need to get back to the "current" state of your project:
$ git checkout master
$ git checkout HEAD [file]
To discard all changes in working directory:
$ git checkout -- [file]
How to reset the file in the STAGING AREA to be the same as the HEAD commit i.e. to unstage a staged file:
$ git reset HEAD [file]
Remember, this command still preserves the changes locally i.e. the working directory isn't affected.
$ git reset --hard HEAD .
Suppose we want to rewind history, and reset our current commit, our working directory, and our staging area to completely match the state of a git repository at a specified commit. Additionally, we want to save these changes. This is how it'll be done:
The short way is to use:
$ git reset --hard [commit-SHA]
Just like stated previously, it resets BOTH the staging area & working directory to match the specific commit. However, this time it also deletes any uncommitted changes, and it also deletes any commits after [commit-SHA] simply because we're "rewinding the clocks". Hint: You can get it back though the reflog command
The slightly longer way will be to use:
$ git reset [commit-SHA]
$ git checkout HEAD .
The first line resets the HEAD to a previous commit in your commit history, resets the staging area to match, but leaves the working directory alone. The second line, as stated previously, resets the file in the WORKING DIRECTORY to the HEAD commit.
Whichever way you decide to reset your files is fine, but remember that any files that were created after the desired commit point will still be present in the working directory. In doing so, those files will be listed as untracked files. To bring everyhing back EXACTLY as how it was in the desired commit, you will have to remove untracked files from the working tree/directory. Here's how you do it:
- To TEST what files will be removed, use
$ git clean -n -dx
- To LITERALLY remove the untracked files, use
$ git clean -f -dx
The
-f -dx
option forces the command to additionally remove directories and ignored files.
The picture above is a good representation of what happens when you run the command. In which case, each circle represents a commit.
Before reset:
- HEAD is at the most recent commit
After resetting:
- HEAD goes to a previously made commit of your choice
- The gray commits are no longer part of your project
- You have in essence rewound the project's history
$ git commit --amend
As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:
$ git commit -m 'initial commit'
$ git add [forgotten_file]
$ git commit --amend
By doing this, you will only end up with one commit, where the new changes will be amended to the previous commit.
To just change the message of the last commit:
$ git commit --amend -m "New commit message"
Git Branching | Back to ToC
Git branching allows users to experiment with different versions of a project by checking out separate branches to work on.
$ git branch
In the output, the * (asterisk) is showing you what branch you’re on.
$ git branch [branch_name]
$ git checkout [branch_name]
$ git checkout -b [new-branch]
The
-b
option is a convenience flag that tells Git to run$ git branch [new-branch]
before running$ git checkout [new-branch]
$ git branch -d [branch_name]
NOTE: Git prevents you from deleting the branch if the branch has unmerged changes.
$ git branch -D [branch]
$ git branch -m [branch]
$ git branch --all
$ git merge branch_name
Remote Directories | Back to ToC
$ git clone
Add
--recursive
option if the remote has submodules.
$ git remote -v
git fetch
$ git merge origin/master
$ git push origin <branch_name>
$ git remote add origin [remote_name]
$ git remote rm [remote name]
Source