User Tools

Site Tools


newdevs:git:commands

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
newdevs:git:commands [2023/12/06 13:15] tmccannanewdevs:git:commands [2025/04/04 13:28] (current) – [Useful Git Commands] tmccanna
Line 35: Line 35:
   * ''git fetch working'' --> fetches all new branches in the working directory   * ''git fetch working'' --> fetches all new branches in the working directory
   * ''git help'' --> access the built-in Git help documentation   * ''git help'' --> access the built-in Git help documentation
-  * ''git log <nowiki>--oneline</nowiki>'' --> list previous commits with their unique ids+  * ''git log'' --> lists most recent commits with details 
 +  * ''git log <nowiki>--name-only</nowiki'' --> lists first line only of most recent commits 
 +  * ''git log <nowiki>--oneline</nowiki>'' --> 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'' --> 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 origin main'' --> import all updates from remote origin repo to local main repo
Line 50: Line 52:
   * ''git reset <nowiki>--hard HEAD</nowiki>'' --> remove last commit   * ''git reset <nowiki>--hard HEAD</nowiki>'' --> remove last commit
   * ''git reset <nowiki>--hard HEAD~2</nowiki>'' --> remove last 2 commits (increment numeral as needed)   * ''git reset <nowiki>--hard HEAD~2</nowiki>'' --> remove last 2 commits (increment numeral as needed)
 +  * ''git revert <hash>'' --> remove a specific commit from a branch that you've applied multiple commits to
   * ''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 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 <hash>'' --> display the commit text and differences of the specified commmit
Line 57: Line 60:
   * ''gitk'' --> use this after committing changes but before pushing them to see what will be pushed   * ''gitk'' --> use this after committing changes but before pushing them to see what will be pushed
  
-==== Rebase ====+===== Rebase =====
  
 If your patch is behind current main, you'll need to rebase it. If your patch is behind current main, you'll need to rebase it.
Line 70: Line 73:
   - Type: git rebase <nowiki>--</nowiki>continue    - Type: git rebase <nowiki>--</nowiki>continue 
  
 +You can also use the interactive rebase mode to look at a series of commits:
 + 
 +  - Open the git branch
 +  - Type: ''git rebase -i origin/main''
 +  - (Optional but recommended) Save this pick list to a separate file, for recovery later if things go wrong
 +  - Choose what to do with each line using the commands listed in the comment block (pick, edit, drop, fixup, squash)
 +  - Save and close the "to do" file
 +  - Follow the prompts to complete each step you specified
  
-==== Squash Commits ====+If you need to start over, just rebase again and paste in your original pick list.
  
-If you are in your local working branch and you've pushed up two commits to the remote git repository, you can combine them:+===== Squash Commits =====
  
-  - Verify that you are in your local branch. +If you have multiple commits in your local branch that you'd like to combine: 
-  - Type: ''git rebase -i origin/main'' + 
-    * Alternatively if your branch is already built on current main, you can use: ''git rebase -i HEAD~4''   (where is the number of commits you wish to combine)    +  - Use 'git logto verify that the commits you want to combine are the most recent 
 +  - Type ''git rebase -i HEAD~2'' (where is the number of commits you wish to combine)
   - Your text editor will open and should show both of the commits, for example:   - 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 014e59c579 LP#1839359 Select element on login not accessible
     * pick 9de92lsi9a LP#1839359 Select element on login not accessible     * pick 9de92lsi9a LP#1839359 Select element on login not accessible
-  - Change the word "pick" in the second line to "fixup" then save and close the file+  - 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     * squash --> merges commits, then allows amendment of commit message
     * fixup --> merges commits like squash does, but discards previous commit message     * fixup --> merges commits like squash does, but discards previous commit message
-  - Push the commit again, and force it to overwrite the previous commits: +  - Use "git commit --amend" if you need to edit the final commit message 
-    * ''git push <nowiki>--force</nowiki> working lp1839359_login_select:user/jdoe/lp1839359_login_select''+  - 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:
  
 +  - Follow the steps above, but when pushing, use "--force"
 +    * ''git push <nowiki>--force</nowiki> working user/jdoe/lp1839359_login_select''
  
-==== Common Bash Commands ====+===== Aliases =====
  
-  * ''cd'' --> move back to the home folder +Shortens normal commands so you type lessYou can add them one at time from the command line:
-  * ''cd foldername'' --> move from the current folder to a child folder +
-  * ''cd ..'' --> move up in the folder hierarchy one level +
-  * ''clear'' --> clears your command window giving you fresh screen to work with (also, Ctrl-l) +
-  * ''cp myfile.txt myfile.bak'' --> copies file with new name +
-  * ''diff myfile.txt otherfile.txt'' --> shows differences between files +
-  * ''echo $ (tab tab)'' --> returns list of all variables +
-  * ''echo $s (tab)'' --> returns list of all variables that begin with 's' +
-  * ''echo (variable name)'' --> returns value of variable +
-  * ''ls'' --> list visible folders and files in the current folder +
-  * ''ls -a'' --> lists all of the files and folders in the current folder, including hidden files +
-  * ''ls -l'' --> lists all of the folders and files in the current folder with additional detail such as last modified timestamp +
-  * ''man (name of command)'' --> opens manual for that command +
-  * ''mkdir newfoldername'' --> create a new folder +
-  * ''mv myfile.txt myfolder'' --> moves file to folder +
-  * ''notepad++ newdocname.txt'' --> create a new file and open it in notepad++ +
-  * ''notepad++ docname.txt'' --> edit an existing file in notepad++ +
-  * ''pwd'' --> see what folder you are currently in +
-  * ''rmdir (myfoldername)'' --> deletes folder+
  
-==== Vim Text Editor ====+<code> 
 +$ 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 
 +</code>
  
-The vim text editor is built into the bash console (similar to notepad in Windows)+You can also edit ''~/.gitconfig'' to see your existing aliases.
  
-  * ''vim'' --> opens vim text editor in command mode +With the above aliases, you would check out main using:
-  * ''vim (filename)'' --> opens file in vim text editor+
  
-Vim opens in command mode, which allows a variety of functions, but does not allow direct editing. +<code>git co origin main</code>
-  * ''i'' --puts you into edit mode +
-  * ''esc'' --puts you back into command mode from edit mode +
-  * '':q'' --> takes you out of vim +
-  * '':w'' --> saves your work +
-  * '':wq'' --> saves & closes +
-  * '':set number'' --> turns on line numbers +
-  * ''?abc'' --> searches for occurrences of 'abc'+
  
 +==== Bash Aliases ====
  
 +Mike Rylander suggests using bash aliases so you can skip typing ''git'':
  
 +<code>
 +# git-related contents of miker's ~/.bash_aliases
 +alias branch='git branch'
 +alias ci='git commit'
 +alias co='git checkout'
 +alias dif='git diff'
 +alias fetch='git fetch'
 +alias lg='git log --pretty=oneline'
 +alias pick='git cherry-pick'
 +alias pull='git pull'
 +alias push='git push'
 +alias stash='git stash'
 +alias status='git status'
 +alias wt='git worktree'
 +</code>
  
 +With these bash aliases in place, you would check out main using:
  
 +<code>co origin main</code>
newdevs/git/commands.1701886500.txt.gz · Last modified: 2023/12/06 13:15 by tmccanna

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.