Git general knowledge
Git is a distributed version control system that tracks changes in source code. It helps enabling collaboration and history management for projects.
The name "origin" is a shorthand name for the remote repository that a project was originally cloned from. It is a standard convention used instead of that original repository's URL, making referencing much easier.
A
.gitignore
file is used to exclude files in the repository. These files wont be part of a commit. Used with personal, configuration or library files.It is best practises to use branches for your own development and merge later instead of committing directly to
main
.Default branch for a repository is called main. Before it was called master, but that has now been deprecated (can be found in old repositories)
Useful sequences
Standard dev sequence
-
Checkout the main branch and pull any updates so that your local copy of the repository is up to date.
git checkout main git pull
-
Create a new branch and start development
git checkout -b new_dev_branch
-
Add any modified or new files into a stage so that they can be committed.
git add new_files
-
Commit files and push to the remote repository
git commit -m 'commit message' git push
Merge with/without conflicts
- Rebase your branch to main - download any updates from main so that your branch is up to date
git fetch origin/main
git merge origin/main
- If no conflicts, then push the changes
git push
- If conflicts are found, then resolve the conflicts and push the changes
git add any_files
git commit -m 'updating my branch'
git push
Top used git commands
git config
- Set username and email for global config
git config --global user.name "username"
git config --global user.email "email"
- Set username and email for local config
git config --local user.name "username"
git config --local user.email "email"
git pull
- Get the latest version of the repository for the current active branch
git pull
- Get the latest version of the repository for a specific branch (main in this case)
git pull origin main
git checkout
- Create a branch for work
git checkout -b work_branch
- Change to a different branch (main for the default repository branch)
git checkout main
- Get just a single file from another branch (if origin/main used, then it will reset the file status to the repository's main branch)
git checkout origin/master -- filename
git branch
- Delete branch locally
git branch -d work_branch
- Delete branch remotely
git push origin --delete work_branch
- Rename branch from main
git branch -m old_name new_name
- Rename branch from the same branch
git branch -m new_name
- Rename the local branch to the new name
git branch -m $old_name $new_name
- Delete the old branch on remote
git push $remote --delete $old_name
- Prevent git from using the old name when pushing in the next step
git branch --unset-upstream $new_name
- Push the new branch to remote
git push $remote $new_name
- Reset the upstream branch for the new_name local branch
git push $remote -u $new_name
git status
- See the status of the repository. Which branch you are in, what files have been modified and what files have been created ad new.
git status
git add
- Stage all changes
git add . --all
git diff
- Differences between commits
git diff --name-only commit_hash commit_hash
- See changes between branches (diff)
git diff master...branch
- Export the differences between two branches into an external file.
git diff branch1..branch2 -- file.txt
git reset
- Discard local changes as only indexed files
git reset –hard
- Discard all the file
git clean -fxd
- Reset branch to the same status as origin
git fetch origin
git reset --hard origin/main
git commit
- Commit changes
git commit -m 'message'
git log
- Commit history/log
git log
- Summarized log to one line
git log --oneline
- List the modified file names in each commit
git log --name-only
- Compact summary with commit message plus filenames with a note whether they are gone
git log --compact-summary
git revert
- Find a previous commit and revert to it
git log --oneline
git revert to_commit_hash
git push
- Push changes to repo
git push origin 'work_branch'
- Push a new branch to remote
git push --set-upstream origin new-branch
git stash
- Save current changes into a dirty directory (push) and then retrieve them later (pop)
git stash push/pop
- Stash work with a message
git stash save 'message'
- Retrieve a specific stash
git stash pop --index n
- List of all stashes
git stash list
- Delete specific stash
git stash drop index
- Delete all stashes
git stash clear
git merge
- Git merge while resolving conflicts (our changes or their changes)
git merge branch --strategy-option ours/theirs
- Abort a merge command
git merge --abort
As always, if you think anything is missing, wrong or can be improved, add your comments.
Top comments (0)