My often used Git commands
Table of contents
- Add, Commit and Push
- Amending the latest commit message
- Amending older commit messages
- Renaming a Git branch
- Stashing and Cleaning
- Get upstream update into your fork repo
1. Add, Commit and Push
The most basic commands that update a repository everytime you push into it.
# Step 1
git add .
# Step 2
git commit # which I prefer because I could add description easily
## OR
git commit -m "refactor: rename a variable"
# Step 3
git push
- To learn more about git-add
- To learn more about git-commit
- To learn more about git-push
2. Amending the latest commit message
To amend a message of the latest pushed commit. Ensure you are in the correct repository and branch, which the latest commit belongs to.
# Step 1: Amend the latest pushed commit message
git commit --amend -m "Your new desired commit message"
# Step 2: Force push and update the latest commit message
git push --force branch-name
3. Amending older commit messages
git rebase -i HEAD~5 will help to achieve that. It's not necessarily to be 5, and it could be the 3rd or higher latest commits.
git rebase -i HEAD~10 # 10th or higher latest commits
git rebase -i HEAD~2 # 1st and 2nd latest commits
4. Renaming a Git branch
Ensure you are on the old branch you want to rename.
git checkout old_name
Provide a new name for the old branch.
git branch -m new_name
Push the newly renamed local branch and reset the upstream branch.
git push origin -u new_name
Lastly, delete the old remote branch.
git push origin --delete old_name
5. Stashing and Cleaning
# Before 'git stash', we have to add all changes in the working directory
git add .
# Then, stash the changes
git stash
# To check your stash list
git stash list
# The numbering depends on the index of list.
git stash apply 0
# Drop a stash, and the numbering also depends on the index of list.
git drop 0
# Clear stash list
git stash clear
- To learn more about git-stash
- To learn more about Stashing and Cleaning
6. Get upstream update into your fork repo
Once upon a time, I was trying to help a few of my team members to fetch the latest update from the main branch of the remote repository. Then, I figured out it would be best to document it nice and precise for future reference.
git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
Let's get started
On your terminal, check your local branches git branch -l or all available branches git branch -a.
Now, we have to check out the branch you want to merge to, which is usually your working branch.
git checkout DESIRED_BRANCH_NAME
Now we merge to your current working branch from the upstream repository.
Note: This approach will not disturb your current commit history.
It's important to specify the command explicitly by doing:
git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME
Now your current working branch is up to date with the original repository, aka the upstream repository.
References
Last edited Apr 3, 2021