Foreword
I assume you familiar with git. You might already use it few times or it's part of your daily job. To make our life easier while working with this version control system, we use GUI programs like SourceTree, Fork to visualize branches, make commits, push changes and perform other types of operations. All GUI clients execute git commands on our behalf to provide a convenient way of managing it. However, I think it's also useful to know command line instructions because of the following reasons:
- GUI programs may perform unwanted side-actions since we don't actually know what is happening under the hood
- GUI programs may get stuck what can result in change loss or other unexpected behavior
- It's possible to speed up the development process in some cases using git command line interface (e.g. cloning a new repo within a seconds)
- There're some problems which are possible to solve using command-line only
- Debugging in linux-like environment (e.g. CI, there's no GUI, command-line only)
- Become GUI - independent
- Be a better developer
I don't want to say that GUI clients aren't useful (they are and I use them as well). The point is to know both and feel comfortable in any environment. Further down in the article I separated git commands based on their context. This should help find instruction quicker depending on the current action you're looking for.
Just as a side note: there is no need to learn all of them. You can bookmark this page and come here whenever you need a specific command. After coming multiple times you'll eventually learn it.
Git commands
Create
git init <repo-name>
- initializes the new repository with the provided name in the current foldergit clone <remote-url>
- downloads a repository. It's also possible to specify an option where to download, for example:git clone <remote-url> <project-folder>
will download a project into a specified folder. The only requirement in this case that specified folder should be empty
Current status
git status
- shows general status (which branch is currently active, list of modified, added or removed and staged files)git branch
- shows which branch is currently active (highlighted) and list of available local branches
History
git log
- shows the history of the commits (including SHA, date and time, author of each commit)
Branching
git checkout -b <branch-name>
- create a new branchgit checkout <branch-name>
- switch to an existing branchgit branch -d <branch-name>
- delete a branch only if it was merged into the main branchgit branch -D <branch-name>
- delete a branch (even if it wasn't merged into the main branch)git merge <branch-name>
- merges specified branch into current branch
Modifying, Staging
git checkout -- .
- revert latest unstaged changes made in the projectgit checkout <filename>
revert the last change made on a specific filegit add .
- stages all modified and newly added filesgit add <filename>
- stage a specific filegit reset
- unstage all staged filesgit reset <filename>
- unstage a specific filegit diff
- shows the difference between modified file and its last version
Moving
git mv <filename>
- changes file name and preserves its history
Removing
git clean -n
- shows a list of untracked files and folders which will be removed (aka dry-run)git clean -df
- remove local untracked files and folders (this will actually delete files and folders).-d
means directories,-f
means files
Stashing (Saving)
git stash list
- shows a list of stashes for the projectgit stash
- stashes all (staged and unstaged) current changesgit stash save "<message>"
- stashes all current changes with specified namegit stash pop
- applies top stash from the list and removes it from the listgit stash apply
- applies top stash from the list without removal from the list
Remote (checking status, adding or changing)
git remote -v
- shows current remote URLgit remote add origin <repo-url>
- this command is used to add a remote for the first timegit remote set-url origin <repo-url>
- this command is used to change existing remote URL
Committing
git commit -m "<message>"
- commit changes (all necessary changes should be staged before)
Pushing
git push
- pushes local commits to the remote repo
Pulling
git fetch
- downloads latest repository historygit pull
- downloads latest repository history and applies changes to the current files
One line rock
git add . && git commit -m "<message>" && git push
- inline command to stage, commit and push all at once
Other
git checkout <rev>~1 <filename>
- Restore deleted file from the commitgit show <rev> | git apply -R
- Revert commit without committing it (the default behavior of revert is actually revert and commit)
Conclusion
I personally mix usage of the command line and GUI programs in my day-to-day code. Most of the time for staging, unstaging, committing and pushing I use VSCode shortcuts since they help me to do the job faster. In other cases like cloning a new repo, changing a remote, branching and merging I exclusively use command line interface. As I mentioned, it's useful to know both and find a happy medium where to use one or another, happy coding :-)