Table of Contents
- Configurations
- Create Clone Repo
- git status
- git add
- git commit
- git log
- git diff
- git push
- git checkout
- git fetch merge pull
- git rebase
- Branches
- Multiple Remotes
- Submodules
- Undo git merge squash
- Tags
- Animate Git History
Useful links:
Configurations
-
Tell git who are you, for the log history
git config --global usern.name "Cecilio García"git config --global user.email "ceciliogarciaquiros@gmail.com"
-
Modify the editor used by git for the commit message
git config --global core.editor "code --wait"Use visual studio codegit config --global core.editor nanoUse nano
- Change name of default branch to “main”
git config --global init.defaultBranch main
- Change style of merge conflicts.
git config --global merge.conflicstyle diff3Show difference between HEAD, common ancestor and incoming branch
- Local files not to be under version control. You don’t need to skip them in
git add.- Add folder name, files to .gitignore, e.g.:
- build/
- *.so
- __pycache__/
- To ignore for all the repositories
git config --global core.excludesfiles $HOME/.gitignoreput in $HOME/.gitignore those files to be globally ignored. E.g.: __MACOSX, .DS_STORE, *.swp, *~
- Add folder name, files to .gitignore, e.g.:
git config --global pull.rebase trueSetgit pull --rebaseas default when doinggit pull
Create Clone Repo
- Create repo locally:
git init(inside a folder with repo name. It creates the folder .git)- To link a new repo to github, create in github a new repo and locally do
git remote add origin git@github.com:Ceciliogq/school2021.git(choose the repo name)
- Clone existing repository:
git clone git@github.com:Ceciliogq/school2021.git(having setup ssh in github first)git clone repo_address custom_repo_name(change repo name when downloading)git clone repo_address .(download all the files in the repo, without the source folder)git clone -b branch_name repo_address(clone only one branch)
- With https you need to enter username and password every time
rm -rf .gitremove all trace of git.
git status
See the current status of the local repo: modified files, (un-)staged files, untracked files, etc.
git status -swill show a reduced version, with one line per file. To the left, a coloured letter will tell the status of the file.
git add
git add filenameAdd one file to the staged areagit add .Add all the files, tracked and untracked in the current foldergit add -uAdd all the modified files which are trackedgit add -p(--ptach)It allows you to choose wich junks of code wants to add. E.g. if a file contains changes that should belong to different commits.git add foldername ':!*.h5'add folder except some kind of files-
git add _Something when there is a merge conflict??? git reset filenameRemove file from the staged area. (You added the wrong file e.g.)-
A alternative is doing
git restore --staged filename git mv oldfilename newfilenamerename a file and put it in the staged area.git rm filenameremove a file and put the change in the staged area.-
Both need to do commit afterwards
- Move local changes to another branch.
I did some changes but then realized they were in the incorrect branch. Stash the local changes, move to the correct branch, and pop them.
git stashgit checkout >correct-branch>git stash pop
git commit
git commit -m "Commit message" filenameWill commit the added changes in filename. This will only in your local repo not yet on the internet. Without -m will promt you to write a log message. If the message contains ‘#number’, it will link to the issue ‘number’- Modify message of last commit
git commit --amend -m "New message"This changes the hash number- For pushed commits use
git push --force remotename branchname
git log
See history of commits
git log -NWill the last N commitsgit log commit1..commit2(without spaces). Will show the commits from 1 to 2 including 2. 1 must be previous to 2.git log --onelineOnly one line per commitgit log --name-statusshow the name of the files changedgit log -pshow the code changes of each commit- Pending git log –name-status origin/master..
git shortlogShow the contributors and their commits-sonly shows the number of commits-nordered by number of commits-eadd the email- Can be combined as
-sne
git log --all --decorate --graphshows a drawing of the tree structure of commits--decorateshows special tags of commits like HEAD--allshows all the refs in .git/refs as commit--graphshow drawing of commit relations
git diff
See difference between working directory and staging area.
git diff --stagedSee what is going to be committedgit diff branch1 branch2orfile1 file2difference between branches, files, commits, etc.- Save git diff in file and see in color
git diff --color source_file > output.txtless -R output.txt
git push
Publish the commits on the internet repo
git pushdefault repo and branch (origin/master). First timegit push -u origin branchgit push reponame branchnamegit push -f(--force)force the commits to be published.git push origin --delete branchnamedelete the branch remotely
git checkout
git checkout commit_hasload specific commit- Switching between branches has now changed to
git switchgit switch branchnamegit switch -c newbranchcreate new branch and change to itgit switch -c newbranch upstream/mastercreate new branch from another branch
git checkout hash -- filenameget back a commit for a specific file- This has now been replaced by
git restoregit restore filenameget last commit for a file, discard all the changesgit restore --source=hash or branch filenamerestore a file to a specific commit or branch
git checkout -b new_branch remote/branchnameclone remote branchgit branch new_branchcreate new branch pointing to current commit
git fetch merge pull
git fetchBring changes from remote to a local staging areagit mergeMerge previous changes to default remote/branch (remote, branch can be changed). The commits has are not changed, it add a “merge commit” and the rest are merged in cronological ordergit merge branch_to_be_mergedThis will include the changes in “branch_to_be_merged” into the current branch where you are “checkout”git merge --squasSquash all the commits in one single merge commitgit pull=git fetch+git merge default/defaultgit pull remote branchpull specific fork and branch
git rebase
For rewriting the git history: change commit order, drop/edit commits, merge multiple commits into one.
git rebase branch1 branch2it will put the commits of branch2 on top of branch1git rebase remote/branchit takes the history of remote/branch and will add the local changes (commits) on topgit pull --rebase=git fetch& previous pointgit rebase -i commit_hashmodify a commit interactively
Branches
git branchsee current branches- Get branch from merge request
git fetch upstream merge-requests/id/head:new_branchgit checkout new_branch- id is the number of the MR and new_branch the name I give to the new branch
- Delete branches
git branch -d localBranchNamedelete local branchgit push origin --delete remoteBranchNamedelete branch remotely
- Discard local changes and bring remote branch
git fetch --allgit reset --hard origin/master
- Rename Branches
git branch -m <oldname> <newname>- To rename the current branch
git branch -m <newname> - To push the local branch and reset the upstream one:
git push origin -u <newname> - Delete the old remote branch
git push origin --delete <oldname>
Multiple Remotes
When you want to contribute to an existing project you typically fork that repo into your username and clone your fork. That is your ‘origin’, but you can other repos and call the whaterver you want. It is common practice to name the second one as ‘upstream’.
- To link your local repo to the upstream one:
git remote add upstream url_of_desired_repo
You can fetch chages from upstream, merge, switch branches and push changes to it.
git remote -vto see the forks availablegit remote rm upstreamto remove one fork- Move branch from one repo to another:
git remote add remoteRepo git@git/https...git checkout branch-to-copygit push reomteRepo
- Set a local branch to track a remote branch. When doing
git pullit will merge to this remote branch.
git branch <branch_name> -u <your_new_remote/branch_name>
Submodules
git submodule add url pathAdd reference to another repository at a certain commitgit clone url --recursiveClone also the submodulesgit submodule update --init --recursiveUpdate submodules (e.g. if changed on the remote)
Undo git merge squash
git fetch upstream(lscsoft/master)git show commit1_hashThis is the commit where my branch diverged from upstream/master after the mergegit revert commit1_hashThis will make a series of commit to undo the merge commitgit rebase -i commit2_hashThis is the previous to commit1- Delete the line of my big merge into master
git merge upstream/master- Solve conflicts if any
git add,git commit,git push -f origin/review-branch
Tags
Tag a specific commit, e.g. for released versions. There are two types of tags:
- Lightweight: only points to a commit
-
Annotated: include more info like a messages and the tagger’s info
git tag tagnameLightweightgit tag -a tagname -m "Version v.1"Annotated-
git tag -a tagname commitMake annotated tag of old commit git tagto see the list of tags-
git tag -l "v1.8*"see the list of tags matching a pattern -
git show tagnameSee info about a tag git push origin tagnamepublish taggit push origin --tagspublish all tags-
git push origin --follow-tagspublish only annotated tags git tag -d tagnamedelete tag in local repogit push origin --delete tagnamedelete tag in remote-
git push origin :refs/tags/tagnamesame than previous git checkout tagnameswitch to a taggit checkout -b new_branch tagnamecreate new branch from tag
Animate Git history
With Gource we can do beautiful animations of the history of a git repository.
The video below shows the history of the LALSuite repo from 2018 to 2021. The specific command run to produce the video was
gource --start-date "2018-01-01" --hide dirnames,filenames --seconds-per-day 0.05 --auto-skip-seconds 1 -1280x720 -o - | ffmpeg -y -r 60 -f image2pipe -vcodec ppm -i - -vcodec libx264 -preset ultrafast -pix_fmt yuv420p -crf 1 -threads 0 -bf 0 gource.mp4.
Enjoy the firecommits!