Git cheatsheet
With this post, I wanted to share my latest Git cheatsheet, with my most used commands and common workflows used at my daily projects.
The Git cheat sheet
What is Git?
"Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different systems).
Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. Since 2005, Junio Hamano has been the core maintainer. As with most other distributed version control systems, and unlike most client–server systems, every Git directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server. Git is free and open-source software distributed under the GPL-2.0-only license." wikipedia
Common Git workflows
In this section I wanted to post common workflow actions, I usually do in my daily work.
Bring changes from other branch onto current
Here is how you would transplant a topic branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto.
Steps:
-
Run the git branch command to make sure you are at the branch that you want to update.
git branchOutputdevelop * master -
Run a git Fetch to update changes available in the remote repository.
git fetch -
Rebase the master branch.
git rebase master --onto developOutputSuccessfully rebased and updated refs/heads/master. -
Push the new changes on the master branch.
git push
Discard local changes
When you change code in one or more files locally, and you realize you want to go back to the latest repository state, the remote state.
-
First we confirm there aren't unstaged files.
git statusoutputOn branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean -
[Optional] If there are unstaged files we run the next command.
git resetoutputUnstaged changes after reset: M PythonGenerators.ipynb -
Next, we overwrite the local changes. We can add --FileName to select a specific file.
git checkoutoutputM PythonGenerators.ipynb Your branch is up to date with 'origin/main'. -
Finally we discard the local changes to all files.
git reset --hardoutputHEAD is now at 9fbe555 all commits except first
Unify commits into one
For this case I pushed 3 commits due to some mistakes until I finlly had the correct files. To clean the last three commits and make them into one you can do the following.
The easy way
-
Get the lastest commit logs.
git log --oneline --graph --decorateoutput* be501b6 (HEAD -> main, origin/main) Files ready * 3f12dce Ups! Readme Updated * ae11087 License and Readme added * 2017231 Init files -
Squash until commit hash 2017231 Init files (not included).
git reset --soft 2017231outputNo output -
Overwrite local changes
git commit -m "License & Readme added"output[main 92c87b4] License & Readme added 41 files changed, 816 insertions(+) ... -
Push the local changes
git push --forceoutput4 files changed, 121 insertions(+), 53 deletions(-)
The manual way
-
Get the lastest commit logs.
git log --oneline --graph --decorateoutput* be501b6 (HEAD -> main, origin/main) Files ready * 3f12dce Ups! Readme updated * ae11087 License and Readme added * 2017231 Init files -
Squash the commits by editor using git rebase.
git rebase -i 2017231 -
After git rebase, you'll see an editor like the output shown. With the selected commits and a list of commands. *Press "i" to start editing the file
pick 92c87b4 License & Readme added pick f96f4e5 Ups! License updated pick 954518c Files ready # Rebase 2017231..954518c onto 2017231 (3 commands) ... -
Change the pick commits to stash commits you can add an "s" at the begining
pick 92c87b4 License & Readme added s f96f4e5 Ups! License updated s 954518c Files ready # Rebase 2017231..954518c onto 2017231 (3 commands) ... -
At the next screen you can type the new commit message for the combination of the three commits. Delete the old comments and type the new one.
# This is a combination of 3 commits. # My final commit message License & Readme added # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. -
To check your new commit combination, run a git log.
git log --oneline --graph --decorateoutput* ec8cc95 (HEAD -> main) License & Readme added * 2017231 Init files
Other references and links
- Git Project
- More articles like this here: Resources


