Git is a distributed version control system that helps developers collaborate on projects of any scale. Linus Torvalds, the developer of the Linux kernel, created Git in 2005 to help control the Linux kernel's development.
Everything starts from here. The first step is to initialize a new Git repo locally in your project root. You can do so with the command below:
git init
The command below returns a list of information about your git configuration including user name and email:
git config -l
With the command below you can configure your user name:
git config --global user.name "Global Open Versity"
This command lets you setup the user email address you'll use in your commits.
git config --global user.email "info@globalopenversity.com"
You can store login credentials in the cache so you don't have to type them in each time. Just use this command:
git config --global credential.helper cache
The command below will add a file to the staging area. Just replace filename_here
with the name of the file you want to add to the staging area.
git add filename_here
If you want to add all files in your project to the staging area, you can use a wildcard .
and every file will be added for you.
git add .
This command will show the status of the current repository including staged, unstaged, and untracked files.
git status
This command will open a text editor in the terminal where you can write a full commit message.
A commit message is made up of a short summary of changes, an empty line, and a full description of the changes after it.
git commit
You can add a commit message without opening the editor. This command lets you only specify a short summary for your commit message.
git commit -m "your commit message here"
You can add and commit tracked files with a single command by using the -a and -m options.
git commit -a -m "your commit message here"
This command shows the commit history for the current repository:
git log
This command shows the commit's history including all files and their changes:
git log -p
This command shows a specific commit.
Replace 'commit-id' with the id of the commit that you find in the commit log after the word commit.
git show commit-id
This command will cause the Git log to show some statistics about the changes in each commit, including line(s) changed and file names.
git log --stat
You can pass a file as a parameter to only see changes on a specific file.git diff
shows only unstaged changes by default.
We can call diff with the --staged
flag to see any staged changes.
git diff
git diff all_checks.py
git diff --staged
This command opens a prompt and asks if you want to stage changes or not, and includes other options.
git add -p
This command expects a commit message to explain why the file was deleted.
git rm filename
This command stages the changes, then it expects a commit message.
git mv oldfile newfile
Create a .gitignore
file and commit it.
git checkout filename
You can use the -p option flag to specify the changes you want to reset.
git reset HEAD filename
git reset HEAD -p
git commit --amend
allows you to modify and add changes to the most recent commit.
!!Note!!: fixing up a local commit with amend is great and you can push it to a shared repository after you've fixed it. But you should avoid amending commits that have already been made public.
git revert
Git revert will create a new commit that is the opposite of everything in the given commit.
We can revert the latest commit by using the head alias like this:
git revert HEAD
You can revert an old commit using its commit id. This opens the editor so you can add a commit message.
git revert commit_id
By default, you have one branch, the main branch. With this command, you can create a new branch. Git won't switch to it automatically – you will need to do it manually with the next command.
git branch branch_name
When you want to use a different or a newly created branch you can use this command:
git checkout branch_name
You can view all created branches using the git branch
command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green.
git branch
In a single command, you can create and switch to a new branch right away.
git checkout -b branch_name
When you are done working with a branch and have merged it, you can delete it using the command below:
git branch -d branch_name
To merge the history of the branch you are currently in with the branch_name
, you will need to use the command below:
git merge branch_name
We can use --graph
to get the commit log to show as a graph. Also, --oneline
will limit commit messages to a single line.
git log --graph --oneline
Does the same as the command above, but for all branches.
git log --graph --oneline --all
If you want to throw a merge away and start over, you can run the following command:
git merge --abort
This command adds a remote repository to your local repository (just replace https://repo_here
with your remote repo URL).
git add remote https://repo_here
You can see all remote repositories for your local repository with this command:
git remote -v
Just replace origin
with the name of the remote obtained by
running the git remote -v command.
git remote show origin
When all your work is ready to be saved on a remote repository, you can push all changes using the command below:
git push
This command shows the name of all remote branches that Git is tracking for the current repository:
git branch -r
This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).
git fetch
Commit after commit, Git builds up a log. You can find out the remote repository log by using this command:
git log origin/main
If the remote repository has changes you want to merge with your local, then this command will do that for you:
git merge origin/main
This lets you update the remote without merging any content into the
local branches. You can call git merge or git checkout to do the merge.
git remote update
If you want to push a branch to a remote repository you can use the command below. Just remember to add -u to create the branch upstream:
git push -u origin branch_name
If you no longer need a remote branch you can remove it using the command below:
git push --delete origin branch_name_here
You can transfer completed work from one branch to another using git rebase
.
git rebase branch_name_here
Git Rebase can get really messy if you don't do it properly. Before using this command I suggest that you re-read the official documentation here
You can run git rebase interactively using the -i flag.
It will open the editor and present a set of commands you can use.
git rebase -i master
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit
This command will force a push request. This is usually fine for pull request branches because nobody else should have cloned them.
But this isn't something that you want to do with public repos.
git push -f
Help. Get help with git commands
git help <git command>
git add . //stage all files with changes after working
orgit add <SPECIFIC_FILE/DIRECTORY>$ git commit //opens interactive shell for type commit message
orgit commit -m "YOUR COMMIT MESSAGE"$ git pull //pull from remote branch tracked by current local branch
orgit pull origin <BRANCH> //BRANCH - master, dev, feature-x...$ git push //if already tracking a remote branch
Put our work to a stack until we pull new changes from remote.
git stash //stash unstaged changes
git stash save "add style to our site" //stash with a message//pull from remote and merge
git stash show //show stash
git stash show -p //show the diff of stash
git stash list$ git stash pop
orgit stash pop stash@{0} //stash@{0} - stash number$ git stash apply
Create a branch from the stash
git stash branch <BRANCH_NAME> stash@{1} //stash@{1} - stash no.
Clean stash
git stash drop stash@{1}
git stash clear //clear all stashes
Useful for tracking releases/versions.
git tag //list tags
git checkout <TAG> //checkout the code at tag(version)
git tag -a v0.0.3 -m "version 0.0.3" //create a new tag v0.0.3
git push --tags
When the files are already identified by git .gitignore cannot remove them. This happens mostly when you are adding a new entry to .gitignore for files which are already identified by git.
git rm -r --cached . && git add . && git commit -m "fixing .gitignore"
// You can run above commands one by one with more control/clarity
git rm -r --cached .
git add .
git commit -m "fixing .gitignore"