git: Commonly used commands
220309
Intro
This is simply a reference list of some of the basic yet commonly used git commands. For further details, you can always refer to git documentation.
Commits
Check the status of the current branch
git status
Stage all changes of trucked files
git add --all or git add .
Commit
git commit -m <"commit-message">
Commit and automatically stage every change of tracked files
git commit -am <"commit-message">
Amend all last changes to the last commit (without to change the commit message)
git commit --amend --no-edit
Apply the changes from the last commit of another branch to the current branch
git cherry-pick <other-branch-name>
Stash changes (record/save the current state of the working directory and revert the working directory to match the HEAD commit – last commit)
git stash
Pop changes from stash
git stash pop
Show the history log
git log --stat or (viewing a graph-like) git log --graph --decorate
Remote Repo(s)
Use gh to authenticate against github.com by reading your token from a file
gh auth login --with-token < <token-file-name> (e.g. ~/ghtoken1.txt)
Use gh to create a new private repo on GitHub
gh repo create <'github-repo-name'> --description <'repo-description'> --private
Add a remote GitHub repo
git remote add <local-name-for-remote-repo> https://github.com/<username>/<repo-name>.git e.g. git remote add github_origin1 https://github.com/zzpzaf/nest-database-raw-queries-api.git
Branches
View all local branches
git branch or git branch --list
View all branches including remote branches
git branch -a
View all branches and their commits
git show-branch -a
Create a branch
git branch <branch-name> or copy the current branch git branch -M <branch-name>
Switch (checkout) to another branch
git checkout <branch-name>
Create and switch (checkout) to another branch
git checkout -b <branch-name>
Create a new branch based on an existing one
git branch <new-branch-name> <existing-branch-name>
Create a new branch from a specific commit
git branch <new-branch-name> <commit-hash>
Rename/Move current branch
git branch -m <new-branch-name> or force it git branch -M <new-branch-name>
Push a local branch to a remote repo (e.g. ‘origin’)
git push -u origin <branch-name>
Push all branches
git push --all <local-name-for-remote-repo>
Delete a branch
git branch -d <branch-name> or force deletion even it has unmerged changes git branch -D <branch-name>
Merge a branch into current branch
git merge <branch-name>
Merge and create a merge commit in all cases (even when the merge could instead be resolved as a fast-forward)
git merge <branch-name> --no-ff
Perform a merge, and commit the result
git merge <branch-name> --commit
Cloning
- A ‘standard’ or default git clone includes everything in a repository (every file and every revision).
- A ‘shallow’ commit allows you to truncate the commit history to just the latest commit only, instead to clone the full/entire commit history
Clone the entire repo with all branches
git clone https://github.com/<username>/<repo-name>.git <folder-name>
Clone the entire repo and switch/checkout to a specific branch
git clone --branch <branch-name> https://github.com/<username>/<repo-name>.git or git clone -b <branch-name> https://github.com/<username>/<repo-name>.git
Use the gh to clone a repo from GitHub
gh repo clone https://github.com/<username>/<repo-name>.git
Clone a specific branch
git clone --branch <branch-name> --single-branch https://github.com/<username>/<repo-name>.git or git clone -b <branch-name> --single-branch https://github.com/<username>/<repo-name>.git or to a specific folder git clone https://github.com/<username>/<repo-name>.git -b <branch-name> --single-branch <folder-name>
Clone just the last commit of a specific branch (‘shallow’ clone)
git clone https://github.com/<username>/<repo-name>.git --depth=1 --branch <branch-name> --single-branch <folder-name>
Other
Prune –> Remove the whole repo history from master
If you have performed a ‘standard’ clone into a folder on your workstation, you can prune it. Here is the list of commands on how to Prune a repo
If you have performed a ‘standard’ clone into a folder on your workstation, you can prune it. Here is the list of commands on how to Prune a repo
git checkout --orphan <new-branch-name> git add -A git commit git branch -D master git branch -m master git push -f origin master git gc --aggressive --prune=all git push -f origin master
First, use the –orphan option, to put the local repo in the ‘init’ state, with only one commit. Then, add all the files in the path and commit. Next, delete the remote master branch, and rename the current branch to master. The next step is to force push the new master to the remote repo (e.g. GitHub hosting environment). After that, remove all the old files using the prune command, and finally, again, push the new state to the remote.
Everyday commands
Finally, bellow are some real examples for everyday work
Git initialization
git init git add . git commit -m "master initial commit"
Create the main branch and checkout
git branch -M main
Login with your token in GitHub
gh auth login --with-token < ~/ghtoken1.txt
Create a new remote repo
gh repo create 'restapidemo_base' --description 'A very simple Spring Boot REST API for demo purposes, serving as the base for further development' --public
Add a remote repo
git remote add github_origin1 https://github.com/zzpzaf/restapidemo_base.git
Remove a remote repo
git remote rm github_origin1
View defined remote repos
git remote -v
github_origin1 https://github.com/zzpzaf/restapidemo-jdbc-mysql.git (fetch)
github_origin1 https://github.com/zzpzaf/restapidemo-jdbc-mysql.git (push)
Push a branch to a defined remote – git push <defined remote> <branch>
git push github_origin1 main
That’s it!
Enjoy, and stay tuned.
Thnx for reading!