Git Cheat Sheet¶
Git installation¶
For GNU/Linux distributions, Git should be available in the standard system repository. For example, in Debian/Ubuntu please type in the terminal:
$ sudo apt-get install git
If you need to install Git from source, you can get it from git-scm.com/downloads.
An excellent Git course can be found in the great Pro Git book by Scott Chacon and Ben Straub. The book is available online for free at git-scm.com/book.
Git configuration¶
Set the name that will be attached to your commits and tags.
$ git config --global user.name "Your Name"
Set the e-mail address that will be attached to your commits and tags.
$ git config --global user.email "you@example.com"
Create a New Project¶
Go into the directory containing the project.
$ git init
Ignoring Files¶
You’ll probably want to create a .gitignore
file right away, to indicate all of the files you don’t want to track.
Verify the .gitignore
file exists in your project and ignore certain type of files, such as all files in logs
directory (excluding the .gitkeep file), whole tmp
directory and all files *.swp
/logs/*
!logs/.gitkeep
/tmp
*.swp
Use git add .gitignore for single ignore file
$ git add .gitignore
To add all of the relevant files
$ git add .
Create a new commit from changes added to the staging area. The commit must have a message!
$ git commit -m "chore: initial commit"
Check conventional commit message format.
Cloning Project¶
Downloads a project with the entire history from the remote repository.
$ git clone <<SSH Url> or <HTTPS Url>>
This example will create folder named aspnet
.
$ git clone git@gitlab.com:pss-x/support/containers/dotnetcore/aspnet.git
Pull branch to specific folder.
$ git clone --branch <Branch-Name> <<SSH Url> or <HTTPS Url>> <Folder-Name>
Git branching model¶
Branch¶
List all local branches in repository. With -a
: show all branches (with remote).
$ git branch -a
You can create a branch locally as long as you have a cloned version of the repository.
$ git branch "new_feature"
Switch to the feature branch to work on it.
$ git checkout new_feature
Instead of two command we can combine command into one single command.
$ git checkout -b "new_feature"
Merge¶
Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.
$ git checkout master
$ git merge "hotfix"
Rebase¶
Rebasing essentially takes commits from one branch and replays them on top of another branch. This creates a linear project history as opposed to the branching/merging approach. Some teams prefer to rebase feature branches onto the main branch before merging.
-
Start a Feature Branch:
-
Work on the Feature:
- Make commits as you work on the feature.
-
Rebase onto Main Branch:
git fetch origin git rebase origin/main or git rebase origin/development
-
Resolve Conflicts (if any):
- Resolve conflicts and continue the rebase.
git add . git rebase --continue
- Resolve conflicts and continue the rebase.
-
Force Push the Rebased Branch:
git push --force-with-lease
-
Create a Merge Request:
- Create a merge request in GitLab to merge the feature branch into the main branch.
Rewrite commit message¶
It is possible to change the message or content of a commit if the original one does not match the required structure, or you want to change it for other reasons.
Depending on whether you want to change only the most recent commit or one or more older commits, and whether these commits are only available locally or have already been pushed, you have different options.
-
In GitLab, check how far back in the commit history you need to go:
-
If you already have a merge request open for your branch, you can check the Commits tab and use the total number of commits.
- If you are working from a branch only:
- Go to Code > Commits.
- Select the dropdown list in the top left and find your branch.
- Find the oldest commit you want to update, and count how far back that commit is.
To change the message of older or multiple commit messages, you need to use this command:
git rebase -i HEAD~n
git rebase -i HEAD~1
- Type
i
to enter INSERT mode, and then start editing the text.
The pick command tells Git to use the commits without change. You must change the command from pick
to reword
for the commits you want to update.
-
Save edited text. Press
Escape
to exit INSERT mode, then type:wq
and Enter to save and exit. -
Change message, highlighted with yellow. Press
Escape
to exit INSERT mode, then type:wq
and Enter to save and exit. -
If a commit needs to be replaced remotely, a
git push --force branch_name
is necessary.
git push --force branch-name
For a detailed description, please see changing a commit message
Related documents¶
A good description that shows how to use amend, reword, delete, reorder, squash and split commits can be found here:
* [Rewriting Git History (Blog Post)](https://www.themoderncoder.com/rewriting-git-history/)
* [Rewriting Git History (Video)](https://youtu.be/ElRzTuYln0M)
Reference¶
- "Git Cheat Sheet and Best Practices" https://www.jrebel.com/blog/git-cheat-sheet