logo
LinkedinGithub

back to articles

November 16th 2024

Git Commands Refresher

Source: https://learngitbranching.js.org/?locale=en_US

Commands

Git Commit

git commit

Git Commit

Note: git commit --amend allows you to modify the most recent commit.

Git Branch and Checkout

git branch newImage

Git Branch

git checkout newImage; git commit

Git Checkout

Git Merge

git merge bugFix

Git Merge

git checkout bugFix; git merge main

Git Merge

Git Rebase

git rebase main

Git Rebase

git rebase bugFix

Git Rebase

Note: The git rebase -i command (e.g., git rebase -i HEAD~4) allows you to interactively reorder commits, edit commit messages, squash commits, drop commits, and more.

Note: You can specify two branches after the git rebase command to rebase one branch onto another. The syntax is:

git rebase <upstream-branch> <working-branch>

Detach HEAD

git checkout C1

Detach HEAD

Relative Refs (^)

git checkout main^

Relative Refs (^)

git checkout C3; git checkout HEAD^; git checkout HEAD^; git checkout HEAD^

Relative Refs (^)

Relative Refs #2 (~)

git checkout HEAD~4

Relative Refs (~)

git branch -f main HEAD~3

Relative Refs (~)

Reversing Changes in Git

git reset HEAD~1

Git Reset

git revert HEAD

Git Revert

Git Cherry-pick

git cherry-pick C2 C4

Git Cherry-pick

Git Tags

git tag v1 C1

Git Tags

Note: If you leave the commit off, git will just use whatever HEAD is at.

Git Describe

Commit Tree

git describe main

v1_2_gC2

git describe side

v2_1_gC4

Note: The output from git describe branch is in the form <closest tag>_<number of commits since tag>_g<abbreviated commit hash>

Git Checkout With Multiple Parents

The number after ^ specifies which parant after a merge commit. So checkout branch^ refers to first parent, checkout branch^2 refers to second parent, etc.

git checkout main^

Git Checkout

git checkout main^2

Git Checkout

git checkout HEAD~; git checkout HEAD^2; git checkout HEAD~2

Git Checkout

git checkout HEAD~^2~2

Git Checkout

Git Clone

git clone

Git Clone

Remote Branches

Remote branches have the special property that when you check them out, you are put into detached HEAD mode. Git does this on purpose because you can't work on these branches directly; you have to work elsewhere and then share your work with the remote (after which your remote branches will be updated).

git checkout o/main; git commit

Git Checkout

Git Fetch

git fetch

Git fetch

Git Pull

git pull

Git pull

Note: This is the same as git fetch; git merge o/main.

Git Push

git push

Git pull

Diverged Work

These are some ways to base your work off of the most recent version of the remote branch:

git fetch; git rebase o/main; git push

or

git pull --rebase; git push

Git pull

git fetch; git merge o/main; git push

or

git pull; git push

Git pull

Remote Tracking

During a clone, git creates a remote branch for every branch on the remote (aka branches like o/main).

You can create a new branch that tracks a specific remote branch using the following command:

git checkout -b <branch> <remote>/<remote-branch>
git checkout -b foo o/main; git pull

Git pull

git checkout -b foo o/main; git commit; git push

Git push

Another way to set remote tracking on a branch is to simply use the git branch -u option. Running

git branch -u o/main foo

will set the foo branch to track o/main. If foo is currently checked out you can even leave it off:

git branch -u o/main
git branch -u o/main foo; git commit; git push

Git push

Push arguments

To push a branch that does not exist on the remote without checking out the branch, you can use the following command:

git push origin <branch>
git checkout C0; git push origin main

Git push

This command allows you to push commits from a local branch (<source>) to a different branch (<destination>) on the remote repository (origin):

git push origin <source>:<destination>
git push origin foo^:main

Git push

It can also create a branch on the remote if it doesn't exist:

git push origin main:newBranch

Git push

Fetch arguments

To fetch a branch that on the remote without checking out the branch, you can use the following command:

git fetch origin <branch>
git fetch origin foo

Git fetch

This command allows you to fetch commits from a remote branch (<source>) to a local branch (<destination>):

git fetch origin <source>:<destination>
git fetch origin C2:bar

Git fetch

It can also create a branch on locally if it doesn't exist:

git fetch origin C2:bar

Git fetch

No arguments mean it downloads all the commits from the remote onto all the remote branches:

git fetch

Git fetch

Oddities of Source

If you leave out <source> for git push origin <source>:<destination>, you can delete a branch on the remote:

git push origin :foo

Git push

If you leave out <source> for git fetch origin <source>:<destination>, you can create a new branch locally:

Git fetch

Pull Arguments

git pull origin foo is equal to git fetch origin foo; git merge o/foo

git pull origin bar:bugFix is equal to git fetch origin bar:bugFix; git merge bugFix

git pull origin main

Git pull

git pull origin main:foo

Git pull

Social Media
GithubLinkedin
Contact me

(617)-301-1620

hubert.huang.business@gmail.com