Git commands Flashcards
Stage file README.txt for commit
git add README.txt
Commit all staged files
git commit
Commit all staged files with the message “Message”
git commit -m “Message”
Commit all files
git commit -a
Commit all files with the message “Message”
git commit -a -m “Message”
Set your name to “Name”
git config –global user.name “Name”
Set your email to Name@mail.com
git config –global user.email Name@mail.com
Initialize a new repository in the current directory
git init
Show the commit history of the current repository
git log
Show all unstaged changes to the repository
git diff
Show all staged changes to the repository
git diff –staged
Show all unstaged changes to the file README.txt
git diff README.txt
Show all staged changes to the file README.txt
git diff –staged README.txt
Stage all files for commit
git add –all
Get the status of all files in the current directory (which are tracked, changed, staged etc.)
git status
Commit the files README.txt and LICENSE.txt
git commit README.txt LICENSE.txt
Commit all .txt files in the project
git commit “*.txt”
Stage all .txt files in the current directory for commit
git add *.txt
Stage the directory src/ for commit
git add src/
Unstage README.txt
git reset HEAD README.txt
Undo the last commit and move all changed files back to staging
git reset –soft HEAD^
Completely undo the last 2 commits
git reset –hard HEAD^^
Add all staged changes to the last commit with the message “Message”
git commit –amend -m “Message”
Link the current repository to the remote repository https://github.com/Name/test.git and refer to it as origin
git remote add origin https://github.com/Name/test.git
Show all remote repositories
git remote -v
Push the main branch to the remote repository known as origin
git push origin main
Pull from the remote repository known as origin
git pull origin
Remove the remote known as origin
git remote rm origin
Clone the remote repository https://github.com/Name/test.git into the current directory
git clone https://github.com/Name/test.git
Clone the remote repository https://github.com/Name/test.git into the current directory under the name Name
git clone https://github.com/Name/test.git Name
Create a new branch called name
git branch name
Change to the branch called name
git checkout name
Merge the branch called name into the current branch
git merge name
Remove the branch called name
git branch -d name
Create a new branch called name and change to it
git checkout -b name
Show all the existing branches and highlight the current one
git branch
Show all remote branches
git branch -r
Show all remote branches of remote repository origin in relation to the local branches
git remote show origin
Delete the branch name of the remote repository origin
git push origin :name
Delete the branch name even if it isn’t fully merged
git branch -D name
Remove all stale branches of the remote repository origin
git remote prune origin
Push the local branch name to the branch main of the remote repository called origin
git push origin name:main
Show all tags
git tag
Change to tag v1.0
git checkout v.1.0
Add a new tag called v1.1 with the description “Message”
git tag -a v1.1 -m “Message”
Push all local tags to the remote repository called origin
git push –tags origin
Get all changes from the remote repository without merging
git fetch
Synchronize all fetched commits with local commits
git rebase
Apply all commits from branch main to the current branch
git rebase main
Continue synchronizing two branches, after having fixed a conflict
git rebase –continue
While synchronizing two branches, skip a commit causing a conflict
git rebase –skip
Cancel an active synchronization of two branches, after running into a conflict
git rebase –abort
Enable colorization of git output
git config –global color.ui true
Show commit history, with every commit only taking up a single line
git log –pretty=oneline
The placeholder for the author date
%ad
The placeholder for the author name
%an
The placeholder for the SHA hash
%h
The placeholder for the subject
%s
The placeholder for the ref names
%d
Show the commit history every commit being formatted with “%h %ad- %s [%an]”
git log –pretty=format:”%h %ad- %s [%an]”
Show the commit history with all the changes made
git log -p
Show the commit history with all the changes made and all the commit descriptions only taking up a singe line
git log –oneline -p
Show the commit history showing additional stats
git log –stat
Show the commit history showing the branch/merge graph
git log –graph
Show the commit history starting a week ago
git log –since=1.week.ago
Show the commit history until 2 hours ago
git log –until=2.hour.ago
Show the commit history between 1 year ago and 4 months ago
git log –since=1.year.ago –until=4.month.ago
Show the commit history between January 2015 March 2016
git log –since=2015-01-01 –until=2016-03-01
Show the difference between 5 commits ago and 2 commits ago
git diff Head~5..Head^^
Compare the branch main with the branch name
git diff main name
Compare the repository during July 2021 with the repository 1 year ago
git diff –since=2021-07-01 –until=1.year.ago
Show all the changes made to README.html together with some of their commit information
git blame README.txt
Stop tracking the file README.txt
git rm –cached README.txt
Use vim as your editor for messages
git config –global core.editor vim
Show all configurations
git config –list
Show which email is set for this repository
git config user.email
Save the command “log –pretty=format: ‘%h %s [%an]’ –graph” as mylog
git config –global alias.mylog “log –pretty=format: ‘%h %s [%an]’ –graph”