User Tools

Site Tools


newdevs:git:commands

New Developers Working Group

Useful Git Commands

  • git add myfile.txt –> stage a specific file
  • git add -A –> stage all modified files in the current directory and subdirectories
  • git add . –> (Note the period) stage all modified files in the current directory but not subdirectories
  • git branch –> list existing local branches
  • git branch mynewbranch –> create a new branch
  • git branch -m oldbranchname newbranchname –> change name of branch
  • git branch -D branchname –> delete a local branch (cannot delete a branch you currently have checked out, so switch to different branch first)
  • git checkout mybranch –> check out an existing branch
  • git checkout -b mynewbranch –> create a new branch based on your current branch and check it out at the same time
  • git checkout -b mynewbranch origin/main –> create a new branch based on your main branch rather than on your currently checked out branch
  • git checkout main –> switch to main branch
  • git checkout – docname.tt2 –> resets a file you've changed back to its original state (like an undo / revert changes command)
  • git cherry-pick <hash> –> apply a specific commit to your local branch
  • git cherry-pick -s <hash> –> apply a specific commit to your local branch with your signoff
  • git cherry-pick -s <first hash>^..<last hash> –> apply a range of commits with your signoff to your local branch
  • git clean -d -i –> interactively delete untracked files you do not want
  • git clean -d -f –> delete untracked folders you do not want
  • git clone git://git.evergreen-ils.org/Evergreen.git –> clones a remote repository
  • git commit –> invoke the default text editor to add a commit message
  • git commit -m "my commit message" –> add brief commit message instead of opening the text editor to add a commit message
  • git commit --amend –> overwrite your last commit message
  • git commit --amend --author='Jon Doe <example@example.com' –> amend commit author
  • git commit --amend --signoff –> view and amend your sign-off branch
  • git commit --amend --signoff <hash> –> view and amend your signoff for a specific commit on your sign-off branch
  • git commit -a –> combine the git add and git commit steps into a single step (does not include newly created files)
  • git config --global -l –> list all global configuration values
  • git config --global keyname "value" –> create a global value
  • git config --global user.email "you@example.com" –> set your email address
  • git config --global user.name "FIRSTNAME LASTNAME" –> set your name
  • git fetch --all –> refresh your local cache from the remote branches (does not download new branches); equivalent to git remote update
  • git config -e –> show git configuration file
  • git fetch working –> fetches all new branches in the working directory
  • git help –> access the built-in Git help documentation
  • git log –> lists most recent commits with details
  • git log --name-only</nowiki'' --> lists first line only of most recent commits * ''git log <nowiki>--oneline –> lists id plus first line only of most recent commits
  • git pull –> imports all updates from your default remote repo to your default local repo (usually, this is equivalent to 'git pull origin main'); pull is equivalent to doing a fetch followed by a merge
  • git pull origin main –> import all updates from remote origin repo to local main repo
  • git pull --rebase origin main –> rebases (rather than merges) new remote changes to your local repository
  • git push working mybranchname –> push changes to the remote working directory
  • git push working --delete mybranchname –> delete a remote branch
  • git push working --force mybranchname –> forces an overwrite on your previous branch
  • git remote -v –> display remote directories
  • git remote show origin –> display remote directories
  • git remote update –> refresh your local cache from the remote branches (does not download new branches); equivalent to git fetch –all
  • git reset HEAD myfile.txt –> unstage a file that has already been staged
  • git reset --hard –> reset a current branch to its original state
  • git reset --hard HEAD –> remove last commit
  • git reset --hard HEAD~2 –> remove last 2 commits (increment numeral as needed)
  • git rm badfile.txt –> delete a file (if the file is being tracked, be sure to add a commit message indicating the file has been deleted)
  • git show <hash> –> display the commit text and differences of the specified commmit
  • git show --stat –> see what your commit will look like before you push it
  • git status –> display status of current branch
  • git version –> displays the installed version of Git
  • gitk –> use this after committing changes but before pushing them to see what will be pushed

Rebase

If your patch is behind current main, you'll need to rebase it.

  1. Open the git branch
  2. Type: git rebase origin/main –> rebases the current branch to main (you can do this when you have your branch checked out and changes committed, but have not yet pushed it)
  3. If there are merge conflict errors, type: git status
  4. Open the file with the problem in your preferred text editor (notepad++, vim, nano, etc.)
  5. Look for merge conflict markers in the file (>>>) and correct the problems
  6. Type: git add (problem file name)
  7. Repeat steps 4-6 for each additional problem file
  8. Type: git rebase --continue

You can also use the interactive rebase mode to look at a series of commits:

  1. Open the git branch
  2. Type: git rebase -i origin/main
  3. (Optional but recommended) Save this pick list to a separate file, for recovery later if things go wrong
  4. Choose what to do with each line using the commands listed in the comment block (pick, edit, drop, fixup, squash)
  5. Save and close the "to do" file
  6. Follow the prompts to complete each step you specified

If you need to start over, just rebase again and paste in your original pick list.

Squash Commits

If you have multiple commits in your local branch that you'd like to combine:

  1. Use 'git log' to verify that the commits you want to combine are the most recent
  2. Type git rebase -i HEAD~2 (where 2 is the number of commits you wish to combine)
  3. Your text editor will open and should show both of the commits, for example:
    • pick 014e59c579 LP#1839359 Select element on login not accessible
    • pick 9de92lsi9a LP#1839359 Select element on login not accessible
  4. Change the word "pick" in the second line to either "fixup" or "squash" then save and close the file
    • squash –> merges commits, then allows amendment of commit message
    • fixup –> merges commits like squash does, but discards previous commit message
  5. Use "git commit –amend" if you need to edit the final commit message
  6. Push the changes up to your remote working directory as normal
    • git push working user/jdoe/lp1839359_login_select

If you've already pushed multiple commits up to your remote working git repository, you can still combine them:

  1. Follow the steps above, but when pushing, use "–force"
    • git push --force working user/jdoe/lp1839359_login_select

Aliases

Shortens normal commands so you type less:

$ git config alias.co checkout
$ git config alias.cp cherry-pick
$ git config alias.br branch
$ git config alias.ci commit
$ git config alias.st status
newdevs/git/commands.txt · Last modified: 2024/08/29 13:34 by sleary

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki

© 2008-2022 GPLS and others. Evergreen is open source software, freely licensed under GNU GPLv2 or later.
The Evergreen Project is a U.S. 501(c)3 non-profit organization.