Written with StackEdit by KANE.
Basically divided into two parts:
- Working and saving locally.
- Uploading to remote repository.
Initializing a repository locally inside decide location.
git init <project-name-or-leave-these-argument-empty>
- Working Directory ( Basically anything present inside the project directory including both files tracked and not tracked by git )
- Staging Area ( Before creating a snapshot i.e. commit changes are send to these staging area )
- Repository ( .git folder contents i.e. files tracked by git )
There is also a fourth state which is remote state but since its actually again a combination of the three-main states so we mainly want to understand these three.
The command to displays the state of the working directory and the staging area is,
git status
It also tells whether there are any commits to make or not. Also if somethings is added to staging area or not.
The command to add files for tracking or changes in the files for tracking by,
git add <filename-or-use-a-period(.)-to-stage-all-files>
Note: If a file was either renamed or deleted from the file system then to stage such changes we have to use
git add -A
orgit add -u
respectively. The -u option is for update and will add all modified files but no new files, however for rename we have to use -A option that does both deletion and addition.
The command to save a snapshot i.e. we make a commit by using the following,
git commit -m "<type-any-message-here-to-label-commit>"
You can use the git commit
without message flag (-m) to open a prompt in your configured editor where you can type a message .
Use the following command for express commit, basically it is roughly a combination of add and commit in one line but it only works if there are no untracked files present in Working Directory.
git commit -am "<message>"
The command to see list of commits in order with commit id and label along with other relevant info,
git log
And if you want to see what a commit has done then you can see using the following command,
git show <commit-id>
Suppose you stagged some changes after a commit and you don't want those changes and go back to last commit point. In order to do that we first have to unstage the changes,
git reset HEAD <file-name-to-unstage>
Now it has just unstaged, but we want to completely remove the changes and reach our last commit point.
git checkout -- <file-name>
We can use the following line of commands to get git-logs in a useful way,
git log --oneline --graph --decorate --all
Now we can instead of writing these long command again and again, we can save out time by making an alias, to do that use following command,
git config --global alias.hist "log --oneline --graph --decorate --all"
You can replace hist
with whatever command-name you want to give to the alias.
So , Now we can just use the following command to get the logs as we wanted.
git hist
Notes: Git saves all its configurations locally on the machine and we can modify them with git-cli using the
git config
. We can specify config for current repo but if we have to change config globally then we have to use--global
.
Use below command to stage a file for deletion. Note that this will also delete the file from the file system.
git rm <file-name>
But If you want to stage the file for deletion without deleting it from file system, use
git rm --cached <file-name>
Ignoring files means files that you don't want git to keep track of, e.g of such files are log files, or even some folders that contain confidential information.
We can tell git which file to ignore by creating a .gitignore
file and adding names/patterns to it.
For e.g. *.log
, node_modules/
, etc
To see changes in files between any two point in history of commits we can use the below command,
git diff <commit-id-one> <commit-id-two>
or
git diff <branch-name> <branch-name>
The second option will compare latest commits on the branch names provided.
If you want to use third-party tools for diff visualization like p4merge then you can download them and set their configuration like these:
git config --global diff.tool=p4merge git config --global difftool.p4merge.path=/usr/local/bin/p4merge git config --global difftool.prompt=falseMake sure to change '/usr/local/bin/p4merge' to your system path location of installed p4merge executable.
After these config changes you can use the graphic tool just by changing diff to difftool in previous command,
git difftool <commit-id-one> <commit-id-two>
Get list of branches,
git branch
To create a branch and switch to it using,
git checkout -b <new-branch-name>
To only switch to a existing branch, use
git checkout <existing-branch-name>
Make sure that you are on the master branch and then use following command to merge a branch with the master.
git merge <branch-name-to-be-merged>
If while merging there is a conflict then you have to manually resolve the conflict. These process is simpified using a mergetool like p4merge.
If you want to use third-party tools for merge visualization like p4merge then you can download them and set their configuration like these:
git config --global merge.tool=p4merge git config --global mergetool.p4merge.path=/usr/local/bin/p4merge git config --global mergetool.prompt=falseMake sure to change '/usr/local/bin/p4merge' to your system path location of installed p4merge executable.
After these config changes you can use the graphic tool just by,
git mergetool
To delete a branch,
git branch -d <branch-name-to-be-deleted>
To add a light-weight tag on a commit,
git tag <tag-name> <commit-id>
or
git tag <tag-name> [These will create a tag on the commit where HEAD is currently referencing]
To add an annotation tag on a commit,
git tag -a <tag-name> <commit-id> -m "Release 1.0"
or
git tag -a <tag-name> -m "Release 1.0" [These will create a tag on the commit where HEAD is currently referencing]
Saving work in progress on hold and do some necessary changes and then come back to the state where we stopped working. These can be done using the stash command.
git stash
These command will create a stash and save it and clean out working directory. Then we do some necessary changes and commit them and then apply stash using below command,
git stash pop
You can also list stash by,
git stash list
To reset with --soft
option,
git reset <commit-id> --soft
Soft commit will preserve the staging area and working directory that took place after the commit id.
Similarly, you can try with option --mixed
and --hard
. The former will preserve atleast the working directory while the latter will wipe both the staging area and working directory.
To undo the reset operation, we can use the reflog to time-travel back to the time before,
git reflog
Then identify the commit id to jump to before the reset,
git reset --hard <commit-id>