Some basic git commands and what they do.
Simple workflow to get started
Let say you have set up your repository, you can access it via ssh or https. You have its link (we usually prefer via SSH, but the commands are the same with both).
You first clone the repository:
# Cloning a repo via ssh git clone firstname.lastname@example.org:sylhare/Type-on-Strap.git
You can do some modification, review them using and prepare your commit
# Review the state of repo git status # Add a the file name `new_file.md` git add new_file.md # Or add everything you've modified git add --All
If you redo
git status, you will see the changes you have added are now
staged. You can commit them and push it to _
# Create a commit with a message using git commit -m "Add new_file.md"
If you can’t push your commit with
git push directly you may need to pull the first few changes first.
Here is how it would look like on the git history:
Pulling while rebasing allow you to avoid some conflict by replaying the commits from older to newer from both the remote branch and the main branch:
# Make sure you have the last version of the repo, the --rebase for a smoother pull git pull --rebase # Push your commit to remote git push
See log of the repository
git log type
q on the terminal. For more magic with
git log check out Magic stats with git
This can be used when you are updating your local repository with the remote one and avoid conflicts. For example when you have changes not committed, and you need to pull before a commit. A basic workflow would be to stash, pull, commit and re apply your stash.
# To save your local changes as 'stash' git stash # To delete a stash git drop <stash sha> # To get back the last modification from stash and delete it git pop # To get the local changes from a stash and keep it git apply <stash sha>
Create a branch
Get the state and branches of the repository:
# See the repo remotely git remote -v # See the branch locally git branch
Create and checkout on a new branch:
# Create the branch git branch patch-1 # Go inside the branch git checkout patch-1
All the commits made on that new branch will stay on that branch. Only when you merge the commit will be added to the main branch:
Create a branch and checkout in one line from a new local one or by tracking an existing remote one:
# Create a branch locally and checkout in one line git checkout -b patch-1 # Add a remote branch and fetch it git checkout --track origin/patch-1
Revert a commit
Commits are identified by a special number, a commit hash (SHA) like
860652a4ab3749a72401b2ceaacf68b27afbc404. It can
also be identified with the first 7 numbers like
To revert you need to use:
git revert 860652a
Which will revert the commit
860652a. The HEAD be at the previous commit state:
You can also revert multiple commits from HEAD using:
git revert --no-commit HEAD~3..
This way you can revert the last 3 commits (The
.. creates the range).
This will not work if some commits are “merge commits”
If you don’t want to use a “revert” commit to remove your change, you can also rebase and drop the commit.
Amend a commit
When you have made a mistake, or you want to modify your last commit. (Usually the one not pushed to master) Try using:
git commit --amend # Amend commit with change of author git commit --amend --author="John Doe <email@example.com>"
It will allow you to change the commit name, and the modifications included in the commit. If you want to modify multiple commits you can also do:
git rebase -i HEAD~4 -x "git commit --amend --author 'John Doe <firstname.lastname@example.org>' --no-edit"
You may need to force push, or if the branch is protected, you will need to temporarily disable that protection, or your changes will be rejected.
Rename multiple commits
# To return three commits back git rebase -i HEAD~3
You will see the commits, use
reword in the one to change. Validate by doing:
git rebase --continue
At the end, to push remote, don’t forget to add
--force. Bear in mind that you’ll mess up the history for whoever had
a previous version. So use with caution.
If your commit started with
# which is by default a comment,
and you want to keep it in your git message during the rebase.
You can change the comment symbol using:
git config core.commentChar ";" # Then put it back with git config core.commentChar "#"
Push only a commit at a time
When you have multiple commits in waiting. Or during/after a rebase. You can select the SHA of the commits you want to push and execute:
git push <remotename> <commit SHA>:<remotebranchname>
Remote folder and repository links
Remove a folder from git but not local:
git rm -r --cached myFolder
Remove a folder from git:
git remote add <remote nickname> https://github.com/user/repository git rm -r one-of-the-directories git commit -m "Remove duplicated directory" git push origin <your-git-branch> # typically 'master', but not always
Send an empty commit to trigger the pipeline:
git commit --allow-empty -m "trigger pipeline"