Git help cheat sheet
Tara N. English-Sweeney
Fintech Technical Writing Team Lead | Champion of Clear Communication and Customer Engagement
While attending the Write the Docs conference, I learned that several folks are trying to learn Git. After you get a basic understanding of Git (see Running basic Git commands and Migrating to Docs-as-Code for a diagram), what do you do when you make a mistake?
This article provides tips to resolve common errors. I will add to this as I encounter other issues (as I jotted these down just hours after the conference ended).
Please add your tips in the comments or send me a message and I will add them.
Also, if you know a better way to fix something or if I made a mistake, please let me know! I am learning just like everyone else.
Delete a local branch
git branch -d <branch>
Delete a remote branch
List all the branches:
git branch -a
You can press Enter to see more branches. Then, press q to exit the list.
Delete the branch:
git push origin -d <branch>
Move a commit from master to a new branch
git branch <new branch name
git reset HEAD~ --hard
git checkout <new-branch-name>>
Undo a commit
Get the hash for the commit. The hash is the SHA-1 hash that identifies the commit.
领英推荐
To do this:
git log
The result will look something like the following:
Revert the commit:
git revert --no-commit <hash>
git commit -m "revert <hash>"
git push
Note: You can run multiple git revert --no-commit <hash> commands in a row.
Move commits from one branch to another
You can use the cherry-pick command to apply commits from one branch to another. This can be tricky, but it can also be very useful.
Get the hashes for the commits that you want to cherry-pick. A hash is the SHA-1 hash that identifies the commits.
To do this:
git checkout <branch that has the commit to cherry pick>
git log
The following is an example of a single commit:
git checkout <main branch - this can be main, develop - depending on your environment>
git pull
git checkout -b <new branch>
git cherry-pick <hash 1>
git cherry-pick <hash 2>
git log
git push
Make sure you apply the cherry-picked commits in the order in which they were committed.
For example, if you the earliest commit in git log is hash ae056b, then df045c, then ce135d, you would list them in this order:
git cherry-pick ae056b
git cherry-pick df045c
git cherry-pick ce135d