Skip to content

Instantly share code, notes, and snippets.

@RicardoBritoBrens
Last active January 1, 2022 23:47
Show Gist options
  • Save RicardoBritoBrens/032aef04986c05822795098b7325687a to your computer and use it in GitHub Desktop.
Save RicardoBritoBrens/032aef04986c05822795098b7325687a to your computer and use it in GitHub Desktop.
Quick reference git commands

Git Commands Tips and Tricks

How to rename branch and delete remote

git branch -m old_branch new_branch # Rename branch locally

git push origin :old_branch # Delete the old branch

git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote

How to Rename messages of an old or recent commits using Git Interactive Rebase

Not pushed commit

To change the message of the most recent commit that has not been pushed to the remote repository, commit it again using the --amend flag.

Navigate to the repository directory in your terminal. Run the following command to amend (change) the message of the latest commit:

git commit --amend -m "New commit message."

What the command does is overwriting the most recent commit with the new one.

The -m option allows you to write the new message on the command line without opening an editor session.

Before changing the commit message you can also add other changes you previously forgot:

git add .
git commit --amend -m "New commit message."
Pushed commit

The amended (changed) commit is a new entity with a different SHA-1. The previous commit will no longer exist in the current branch. Generally, you should avoid amending a commit that is already pushed as it may cause issues to people who based their work on this commit. It is a good idea to consult your fellow developers before changing a pushed commit.

If you changed the message of most recently pushed commit, you would have to force push it.

Navigate to the repository. Amend the message of the latest pushed commit:

git commit --amend -m "New commit message." Force push to update the history of the remote repository:

git push --force branch-name Changing an Older or Multiple Commits If you need to change the message of an older or multiple commits, you can use an interactive git rebase to change one or more older commits.

The rebase command rewrites the commit history, and it is strongly discouraged to rebase commits that are already pushed to the remote Git repository.

Navigate to the repository containing the commit message you want to change. Type git rebase -i HEAD~N, where N is the number of commits to perform a rebase on. For example, if you want to change the 4th and the 5th latest commits you would type:

git rebase -i HEAD~5 The command will display the latest X commits in your default text editor:

pick 43f8707f9 fix: update dependency json5 to ^2.1.1 pick cea1fb88a fix: update dependency verdaccio to ^4.3.3 pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2 pick c5e078656 chore: update dependency flow-bin to ^0.109.0 pick 11ce0ab34 fix: Fix spelling.

"#" Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands) Move to the lines of the commit message you want to change and replace pick with reword:

reword 43f8707f9 fix: update dependency json5 to ^2.1.1 reword cea1fb88a fix: update dependency verdaccio to ^4.3.3 pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2 pick c5e078656 chore: update dependency flow-bin to ^0.109.0 pick 11ce0ab34 fix: Fix spelling.

"#" Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands) Save the changes and close the editor.

For each chosen commit, a new text editor window will open. Change the commit message, save the file, and close the editor.

fix: update dependency json5 to ^2.1.1 Force push the changes to the remote repository:

git push --force branch-name

How to go back an amount of commits

Go back in time, throwing away changes

git reset --hard HEAD~3

Set HEAD to point to an earlier commit

git reset --soft HEAD~3

Wipe out differences in the working tree

git reset --hard

How to push specific Commit

git push origin {tagName}

Syncrhonize remote tags with local

--prune or -p After fetching, remove any remote tracking branches which no longer exist on the remote.

Step 1 git fetch -p -P

Step 2 git fetch --prune --prune-tags

Step 3 git fetch -p -P origin

Step 4 git fetch --prune --prune-tags origin

Daily Git commands

Full command Alias config into file .bashrc Use
git status alias gs='git status' gs
git commit alias gc='git commit -m $2' gc 'Adding integration test cases'
git push alias gp='git push' gp
git add and git commit alias gac='git add . && git commit -m $2' gac 'Adding integration test cases'
git push --set-upstream origin alias gpst='git push --set-upstream origin $1'

Quick setup github

or create a new repository on the command line

echo "# DigitalClock" >> README.md git init git add README.md git commit -m "first commit" git branch -M main git remote add origin https://github.com/RicardoBritoBrens/DigitalClock.git git push -u origin main

or push an existing repository from the command line

git remote add origin https://github.com/RicardoBritoBrens/DigitalClock.git git branch -M main git push -u origin main

@RicardoBritoBrens
Copy link
Author

Update information

@RicardoBritoBrens
Copy link
Author

Add git reset command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment